Ad

Pass Data From Request Object To Template Node.js

- 1 answer

I am using the request package to make GET requests. I am trying to pass data from the request to my template as a callback but I keep getting the error res.render is not a function. Here is my code:

module.exports = function (req, res) {
request("http://localhost:3000/api/employee", function(err, res, body) {

    var apiResponse = JSON.parse(body);
    var context = {
        name: apiResponse[0].first_name
    }
    res.render('../views/home', context);
});
};

Can someone help?

Thanks in advance!

Ad

Answer

module.exports = function (req, res) {
request("http://localhost:3000/api/employee", function(err, response, body) {

    var apiResponse = JSON.parse(body);
    var context = {
        name: apiResponse[0].first_name
    }
    res.render('../views/home', context);
});
};

Please use this code. The issue in your code is that there are two res variable, res.render shoud use the first res.

Ad
source: stackoverflow.com
Ad