javascript - Return variable value from request.get inside one function to another NODEJS express -
i calling function request.gets json , fills in variables router.get
the variables value inside callfunc function undefined in router.get
how make them global can assign value inside request.get , use value in router.get
var title; var headline; router.get('/test/code/:code', function(req, res, next){ var procedure = "execute procedurename 999, 'username', "+req.params.code callfunc(procedure) res.render('display', { title: title, // <-- value here undefined descritpion: headline, // <-- value here undefined var1: 'block sidebar', var2: 'block content', image: 'http://baidun.com/wp-content/uploads/2013/06/si_rm_1070bw-900x598.jpg', url: 'https://www.youtube.com/watch?v=zb_vpdxahku' }) }) function callfunc(procedure){ request.get('http://mywebservice.com/myservice.asmx/myservicedb?callback=&username=username&procedurename='+procedure, function(req, res, body){ var testvalue = body.slice(1, -2); var result1 = json.parse(testvalue); var result2 = json.parse(result1); title = result2.jobselect[0].jobtitle; headline = decodeuri(result2.jobselect[0].jobheadline); console.log(title) // <-- right value here console.log(headline) // <-- right value here }) }
a way using res.render call back.
var title; var headline; router.get('/test/code/:code', function(req, res, next){ var procedure = "execute procedurename 999, 'username', "+req.params.code callfunc(procedure, function(title, headline) { res.render('display', { title: title, descritpion: headline, var1: 'block sidebar', var2: 'block content', image: 'http://baidun.com/wp-content/uploads/2013/06/si_rm_1070bw-900x598.jpg', url: 'https://www.youtube.com/watch?v=zb_vpdxahku' }) }) }) function callfunc(procedure, cb){ request.get('http://mywebservice.com/myservice.asmx/myservicedb?callback=&username=username&procedurename='+procedure, function(req, res, body){ var testvalue = body.slice(1, -2); var result1 = json.parse(testvalue); var result2 = json.parse(result1); title = result2.jobselect[0].jobtitle; headline = decodeuri(result2.jobselect[0].jobheadline); cb(title, headline); }) }
Comments
Post a Comment