0

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

2
  • 1
    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. Commented Jul 1, 2018 at 22:08
  • 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 Commented Jul 1, 2018 at 22:16

1 Answer 1

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.
        }
    })    
}
Sign up to request clarification or add additional context in comments.

7 Comments

return body in the function next(....) is not working. I need to return the JSON received from the request
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.
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 :)
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.
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
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.