Return json body in REQUEST nodejs

痞子三分冷 提交于 2020-08-21 19:30:30

问题


I'm using the request module to make an HTTP GET request to an url in order to get a JSON response.

However, my function is not returning the response's body.

Can someone please help me with this?

Here is my code:

router.get('/:id', function(req, res) {
  var body= getJson(req.params.id);
  res.send(body);
});

Here is my getJson function:

function getJson(myid){
  // Set the headers
  var headers = {
   'User-Agent':       'Super Agent/0.0.1',
   'Content-Type':     'application/x-www-form-urlencoded'
  }
  // Configure the request
  var options = {
    url: 'http://www.XXXXXX.com/api/get_product.php',
    method: 'GET',
    headers: headers,
    qs: {'id': myid}
  }

  // Start the request
  request(options, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    return body;
  }
  else
    console.log(error);
  })
}

回答1:


res.send(body); 

is being called before your getJson() function returns.

You can either pass a callback to getJson:

getJson(req.params.id, function(data) {
    res.json(data);
});

...and in the getjson function:

function getJson(myid, callback){
// Set the headers
var headers = {
'User-Agent':       'Super Agent/0.0.1',
'Content-Type':     'application/x-www-form-urlencoded'
}
// Configure the request
var options = {
url: 'http://www.XXXXXX.com/api/get_product.php',
method: 'GET',
headers: headers,
qs: {'id': myid}
}

// Start the request
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
    callback(body);
}
else
    console.log(error);
})  

}

or simply call:

res.json(getJson(req.params.id));



回答2:


The problem is that you are doing a return, expecting that the router will get the content.

Since is an async callback, that will not work. You need to refactor your code to be async.

When you are doing return body; the function that is being returned is the callback of request, and in no part you are sending the body to the router.

Try this:

function getJson(myid, req, res) {
  var headers, options;

  // Set the headers
  headers = {
    'User-Agent':       'Super Agent/0.0.1',
    'Content-Type':     'application/x-www-form-urlencoded'
  }

  // Configure the request
  options = {
    url: 'http://www.XXXXXX.com/api/get_product.php',
    method: 'GET',
    headers: headers,
    qs: {'id': myid}
  }

  // Start the request
  request(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      res.send(body);
    } else {
      console.log(error);
    }
  });
}

And this router:

router.get('/:id', function(req, res) {
  getJson(req.params.id, req, res);
});

Here, you are instead passing the res param to the getJson function, so the callback of request will be able to call it as soon as its able to do it.



来源:https://stackoverflow.com/questions/26721994/return-json-body-in-request-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!