简体   繁体   中英

Route to the view received by Node.js request

Controller code:

bina: function(req, res) {
  var request = require('request');

  request({
    url: 'http://localhost:3000/bina/',
    method: 'GET',
  }, function(err, res, body) {
    if (err) {
      console.log(err);
    } else {
      var data = JSON.parse(res.body);
      console.log(data);
      res.render(data)
    }
  })
} 

The data that comes with the request does not see any functions like res.view or res.render .

Error output:

res.render(data)
           ^
TypeError: res.render is not a function

Note:

I can see the data via console.log(data) via web service. I use Sail.js.

Here's what you are doing wrong:

You are using same variable ( res ) name twice. And at the res.render it taking the one which is in the nearest scope. I have renamed the res => to response and now it should work.

bina: function(req, res) {
    var request = require('request');

    request({
        url: 'http://localhost:3000/bina/',
        method: 'GET',

    }, function (err, response, body) { // Changed the variable name
            if (err) {

                console.log(err);

            }
            else {
                var data = JSON.parse(response.body);
                console.log(data);
                res.render(data)

            }
        })
}

Hope it solved your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM