0

i am trying to get access token from api...but i am getting undefined json data pls help.. I got correct data in requestToken() but when it returns data to route.js..

access.js ----------------------------------------------------------------------------------------------------------------------

exports.requestToken = function () {

    var options = {
        url: '---URL---',
        method: 'POST',
        form: {
            access_key: 'xxxxxx',
            secret_key: 'xxxxxx',
            app_id: 'xxxxx',
            device_id: 'xxxxx'
        }

    };

    request(options, function(error, res, body) {
       //console.log(JSON.parse(body)) ---> correct data

      //here s problem    
       return JSON.parse(body);          
    });
    }

route.js -------------------------------------------------------------------------------------------------------------------------

route.get('/get', function(req, res) 
{                                                                       //undefined                                                         
        body = jsonBody.requestToken();

        console.log(body);
        body = jsonBody.reqRecentMatches(token);
        //console.log(body);
        //res.json(body);
}
Manwal
  • 22,117
  • 10
  • 57
  • 89
Rohit
  • 17
  • 7
  • You can't directly return the result from an asynchronous `request()` operation. You will have to restructure your `requestToken()` to either return a promise or to accept a callback that can be called when the result is ready and then restructure the caller of `requestToken()` to work that way. Full discussion of the issue is in the question yours has been marked a duplicate of as this is a regularly asked question here. – jfriend00 Apr 18 '16 at 07:02
  • You should do it in asynchronous way! – vkstack Apr 18 '16 at 07:06
  • thanks sir, i was searching for solution since last 2 days.. but didnt know what exactly was problem in my case. now i found solutions thanks to you – Rohit Apr 18 '16 at 07:21

0 Answers0