0

I'm still new to Node.JS and I'm trying to call a REST API using GET method. I use 'request' package from this link. The call actually works but when I try to return the response body from other .JS file, I get 'undefined'.

Here is my 'first.js'

var request = require('request');
var first = function (){
};
first.prototype.getValue = function (value){
    var thpath = somePath + value;
    var postheaders = {
        'Content-Type': 'x-www-form-urlencoded'
    };
    var options = {
        url : 'https://' + thpath,
        method : 'GET',
        headers : postheaders,
    };
    var data_rec = "";
    request(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            data_rec = JSON.parse(body);
            console.info(data_rec);
            return data_rec;
        } else return "failed to get";
    });
};
module.exports = first;

Here is my 'second.js'

var first = require('./first');
var instance = new first();
var val1 = instance.getValue(someValue);
console.info(val1);

The 'console.info(data_rec)' from 'first.js' returns a JSON (which means that the call is working). However, 'console.info(val1)' from 'second.js' returns 'undefined'. Can anybody find the solution?

Update: I am able to get the solution by using sync-request package.

kosoadi
  • 15
  • 1
  • 5

2 Answers2

0
var val1 = instance.getValue(someValue);
console.info(val1);

instance.getValue is an asynchronous method. Meaning javascript vm wont wait for response, it just goes on with next line ie console.info(val1); Ajax request takes time and response comes in only after some time, and triggers the success function.Till then val1 is undefined

request(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        data_rec = JSON.parse(body);
        console.info(data_rec);
        return data_rec;
    } else return "failed to get";
});

See that console.info(data_rec); is given inside the success function, success function is something that get called after response is successfully retrieved

Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42
0

You're trying to return asynchronous response in a synchronous manner which is not possible. When you make the call, first() is executed and returned with no value and then async call completes. So console.log prints correctly, but you won't receive the value.

Modify this part of code

request(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            data_rec = JSON.parse(body);
            console.info(data_rec);
            return data_rec;
        } else return "failed to get";
    });

to this

var request = request(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            data_rec = JSON.parse(body);
            console.info(data_rec);
        }
    });
return request.then(function (res) {
    if (res.statusCode >= 300) {
      return "failed to get";
    } else {
      return res.body.toString()
    }
  })
Bikas
  • 2,709
  • 14
  • 32