Reputation: 357
I am using Express in Nodejs in order to make a GET Call. As a response, I get the value that I need, but when I return the value from my function to the main I get undefined.
function getInfo()
{
var oauth = getoAuth();
request.get({
url:'myurl',
oauth:oauth,
qs:null,
json:true
},
function (e, r, data) {
body.data = data;
body.emit('update');
});
body.on('update', function () {
console.log(body.data.issues[0].key);
return (body.data.issues[0].key);
});
}
This key is what I need. When I print it in the console I get the correct value, but it doesn't return anything, because it is an async call. How can I return the value? Can I somehow wait for the value using express? I saw on stackoverflow that some people used this body.on('update'... solution, but it didn't work for me. It still saves nothing to a variable.
TESTING: undefined
THEKEY1
var myid= geInfo();
Upvotes: 2
Views: 1719
Reputation: 13679
try this way :
function getInfo()
{
var oauth = getoAuth();
request.get({ url:'myurl', oauth:oauth, qs:null, json:true},
function (error, response, body) {
if(!error && response && response.statusCode==200){
console.log(body);
return body.data.issues[0].key;
}else{
return null;
}
});
}
Upvotes: 1