-2

So, long of the short, I am building a bot with node.js. Part of what I want to do is use an API from a website and set parts of the JSON file that is returned to a variable (although in my example I use the entire return to simplify my explanation/question). I use request() to access the API and if something is returned, I set the variable to the returned JSON information.

I know that the code is properly returning the JSON information, because I can console.log() the JSON file out with the variable. Key thing is that the console.log() of this variable has to be done within request(). If I try to console.log() outside the request() function, it simply logs "undefined".

I have searched around and can tell this has something to do with this being an async call. While that is great to know, I have yet to find a way to work around this issue.

Below is a simplified version of the request() code (with the website missing because I use a key to access the API). I would just like to access the variable outside the request so that I can use it for something else.

Please note the commented out console.log() comments. Code here:

var parsedData;
request("WEBSITE HERE", function(error, response, body) {
    if(error){
        console.log("SOMETHING WENT WRONG!");
        console.log(error);
    }else{
        if(response.statusCode==200){
            parsedData = JSON.parse(body);
            //console.log(parsedData); works here and prints JSON data.
        }
    }});
//console.log(parsedData); here console.log() is undefined.

To the individual claiming this is a duplicate in the link, it is not. I am asking how to use a variable outside this function and a solution was not brought up in the link provided. On top of that, it isn't node.js, although it is Javascript. Overall, it shares a similarity with async calls, but it is not the same question.

user7977797
  • 73
  • 1
  • 1
  • 10
  • Isn't this against the async principles. The console log outside the function gets executed immediately after request is called. But the callback hasn't returned yet. So expecting to see the value means you don't understand difference between javascript and a sequential scripting language – Tarun Lalwani Mar 29 '18 at 02:58
  • I am using console.log() as an example. Ultimately I want to actually use parased Data outside the request() function. I know why it is undefined. It executing before request() finishes. I get this is because of a async call. I want to find a workaround to this. – user7977797 Mar 29 '18 at 03:00
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Aluan Haddad Mar 29 '18 at 03:36
  • This is not a duplicate. This is about node.js, it is about calling from a website and using variables outside that function. Your link does not provide that information. – user7977797 Mar 30 '18 at 02:20

1 Answers1

1

request is one of the most popular NPM module for making HTTP requests. It supports both HTTP and HTTPS and follows redirects by default.

request.get expects an url as a first argument and a callback as a second argument. That means callback function gets called after request.get successfully completed. So thats why, (error, response, and body) has valid information inside callback. This callback gets called after request.get is executed, which can take time, since its asynchronous call.

To store response in local variable check this

Swati
  • 577
  • 6
  • 15