I am currently doing some stuffs in NodeJS and now I have the following issue:
When I get a JSON Object from a HTTP Request and want to return it, it's showing "undefined".
Here is my not working NodeJS Code:
function verifyUser(uname,pword){
var options = {
url: 'CENSORED',
method: 'POST',
headers: headers,
form: {'Username':uname, 'Password':pword, 'Key':key}
}
request(options,function(error,response,body){
if(!error && response.statusCode == 200){
return body;
}
})
}
var test1 = verifyUser("RobDeFlop","CENSORED");
console.log(test1);
But when I replace the return with a console.log its showing me the json object.
I hope someone can help me :)
requestis called byrequestonce the response returns. Returning a value from that function is not the same as returning a value fromverifyUser. You need to passverifyUsera callback or return a promise.function callback(error,response,body){ if(!error && response.statusCode == 200){ return body; } } Request(options,callback);but still does not work