-1

I have a function that returns an object, like this:

function getJSON(url) {
    request.get({
        url: url,
        json: true,
        headers: { 'User-Agent': 'request' }
    }, (err, res, response) => {
        if (err) {
            console.log('Error:', err);
        } else if (res.statusCode !== 200) {
            console.log('Status:', res.statusCode);
        } else {
            // JSON received successfully
            return response;
        }
    });
}

The function is working fine, but when I declare a variable to use what the function returns, it is undefined instead. I expect it to be an object.

var someVar = someFunction('url-to-the-json');
Nathan
  • 1,074
  • 9
  • 14

1 Answers1

-1

Try assign function to variable:

var myVar = function someFunction() {
    //do something here
};
//then use myVar
function newFunction(myVar){
  //do something
 }
Oleg Markoff
  • 321
  • 2
  • 7
  • I'm puzzled by this. You are passing a function to a function? What does this have to do with the OP's question? What do you think accounts for the problem he is reporting? –  May 14 '17 at 06:56