NodeJS - Return a JSON from a request


NodeJS - Return a JSON from a request



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".


"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 :)


return


console.log





The function you pass to request is called by request once the response returns. Returning a value from that function is not the same as returning a value from verifyUser. You need to pass verifyUser a callback or return a promise.
– Mark Meyer
Jul 1 at 22:08



request


request


verifyUser


verifyUser





Can you give me one example for a callback? I added this: function callback(error,response,body){ if(!error && response.statusCode == 200){ return body; } } Request(options,callback); but still does not work
– RobDeFlop
Jul 1 at 22:16



function callback(error,response,body){ if(!error && response.statusCode == 200){ return body; } } Request(options,callback);




1 Answer
1



Ah, the joys of learning async js in node for the first time :3



As @Mark_M mentioned, your function in request is only called after the request is processed. As a result, you can't return a variable from your verifyUser() function. verifyUser() returns immediately once it has SENT the request, and calls the funciton in request() once it has received an answer.



Ideally, you should follow the async flow by providing a callback function:


//We'll define some function called 'callback'
function verifyUser(uname,pword, callback){
var options = {
url: 'CENSORED',
method: 'POST',
headers: headers,
form: {'Username':uname, 'Password':pword, 'Key':key}
}
request(options,callback);
// Here I've changed your inline callback function to the one passed to verifyUser as an argument.
}


// Then, your main code:
verifyuser("RobDeFlop","CENSORED", next);
function next(error,response,body){
if(!error && response.statusCode == 200){
//Do useful stuff with the body here.
}
})
}





return body in the function next(....) is not working. I need to return the JSON received from the request
– RobDeFlop
Jul 1 at 22:56


return body


function next(....)





Rob, I've absolutely been in your situation before. I wanted to return something from an async call to a server - but that's just not a thing in nodejs. The best you can do is use a callback function, and inside that callback function you're guarunteed to have access to the body.
– Thornkey
Jul 2 at 6:11





This is just how Node.js works with async calls. The reason is because: say your request threw an error in the callback. Then it doesn't really make sense to return a body, because there's no body, just an error. Yeah, it's a bit annoying. I came from using C++ and JS in my browser where everything was nice and synchronous - after being tripped up by node async i too threw node out the window for a few days. I hope your temprament is better than mine :)
– Thornkey
Jul 2 at 6:14





So there isnt any possibility to store the JSON Object in any variable? Or like creating new vars with other content, without body? I should throw it out of my window too.
– RobDeFlop
Jul 2 at 10:48





Yeah, but I suspect you'll find many other request/response systems (even AJAX) have this feature, so its good to practice using callbacks :3
– Thornkey
Jul 2 at 11:30






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?