0

I'm new to Java Script and here's what I'm stuck with. I've been trying to make a variable inside my function global so that I could use it in the other parts of the code. Nothing seems to be working so far. Below is my code:

var json2="-";

var request = require("request");
var smallpie1 = 
"https://s3.amazonaws.com/vecareclientjson/user1/predictions.json";

var pre = {rejectUnauthorized: false,
       url: smallpie1,
       method: 'GET',
       json: true
};
function test1(){
    request(pre,function (error,response,body){
        json2 = JSON.stringify(body);
        console.log(json2);
    });
};
console.log(json2);

Output:
-
[Done] exited with code=0 in 0.231 seconds

I was expecting the content in the json to overwrite the json2 object. The goal is to make the json2 object inside the function test1() global.

1
  • 2
    Do you ever run test1() ? Commented Dec 10, 2018 at 17:27

3 Answers 3

1

As other contributers are telling you, you must run the test1() function. You can do so by adding it to the bottom of your code, before logging the json2 variable, such as:

var json2="-";

var request = require("request");
var smallpie1 = 
"https://s3.amazonaws.com/vecareclientjson/user1/predictions.json";

var pre = {rejectUnauthorized: false,
       url: smallpie1,
       method: 'GET',
       json: true
};
function test1(){
    request(pre,function (error,response,body){
        json2 = JSON.stringify(body);
        console.log(json2);
    });
};

test1(); // Here you call the function, which will modify json2
console.log(json2); // Here you print json2 current value
Sign up to request clarification or add additional context in comments.

Comments

0

It seems that you have not yet run the function.

Comments

0

Run the function test1 before console.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.