javascriptnode.jsexpressnpm-request

Get data from request npm


I'm building an app using express js and using request(v-2.88.2) to get data from an api

request(url, function(error, request, body) {
    var data = JSON.parse(body);
});

I want to use var data in other functions.

Are there any ways to do this?


Solution

  • hi you can do this by making you variable as global. its not really good method but we can do this

    var data; 
    request(url, function(error, request, body) {
    data = JSON.parse(body);
    });
    

    by this, you can even access your data variable outside of your function too. hope this may help you.