0

I have created one NODE JS URL request to get JSON and pass it to the new variable var1.

Var1 can get data within request function when call var1 outside of request it returns null value why? how to get data from URL request and pass it to a new variable outside the Request URL?

function datadata() {
    var var1 = 0;
    var request = require('request');
    request('http://x.x.x.x/json', function (error, response, body) {
      if (!error && response.statusCode == 200) {
         var importedJSON = JSON.parse(body);
         var1 = importedJSON.result.data;
         // show value of var1
         console.log(var1); 
      }
    });
    // cannot show value of var1
    console.log(var1);
    return var1;
 } 
Dam Yi
  • 3
  • 2
  • 1
    Does this answer your question? [How to return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Ramesh Reddy Jun 21 '21 at 06:36

3 Answers3

0
  • you can't get something outside the request that easy anyway you can try to use Promise

Example

const request = require('request');
var p = new Promise((resolve, reject) => {
   request('http://x.x.x.x/json', function (error, response, body) {
      if (!error && response.statusCode == 200) {
         var importedJSON = JSON.parse(body);
         resolve(importedJSON.result.data)
      }
});
p.then((data) => {
   console.log(data);
})
JS_INF
  • 467
  • 3
  • 10
0
function datadata(){
    var var1 = 0;
    var request = require('request');
    return new Promise(function(resolve, reject){
      request('http://x.x.x.x/json', function (error, response, body)  {
        if (err) return reject(err);
        try {
          if (!error && response.statusCode == 200) {
            var importedJSON = JSON.parse(body);
            var1 = importedJSON.result.data;
            console.log(var1); 
            } catch(e) {
                reject(e);
            }
        });
    });
}

datadata().then(function(res) {
    console.log(res);
}).catch(function(err) {
    console.err(err);
});
Hamada
  • 1,836
  • 3
  • 13
  • 27
0

You can use a callback to the datadata like so
Check whether the body is found on the response, in that case change your code to

request('http://x.x.x.x/json', function (error,response) {

and

var importedJSON = JSON.parse(response.body);

You should do this because your importedJSON may not be having data because according to your code it's data comes from (body)
Mostly likely the json data will be found on the response.body.
Then we callback the specific data you want, in your case it's result.data. Again make sure the result.data can be found on the raw json data that the request gives you otherwise it won't output anything because there's nothing to output, most likely you will get errors

function datadata() {
    var var1 = 0;
    var request = require('request');
    request('http://x.x.x.x/json', function (error,body,response) {
      if (error && response.statusCode == 200) {
          callback("There was an error") 
      }else{
         var importedJSON = JSON.parse(body);
         callback(importedJSON.result.data);
         // show value of var1
         console.log(var1); 
    });
 } 
datadata((error, var1) => {
   if(error){
      return console.log(error)
    }
      console.log(var1
})
OBrien Evance
  • 704
  • 5
  • 18