Bryan Wheeler
Bryan Wheeler

Reputation: 125

Node.js & Express.js/Jade res.render returns res.render is not a function

I haven't been able to find an answer to this error as of yet and am wondering if it has to do with version 4 of Express

controller.js

var request = require('request');

var apiOptions = {
    server : "http://localhost:3000"
};
if(process.env.NODE_ENV === 'production') {
    apiOptions.server = "https://heroku_path";
}



module.exports.homelist = function(req, res) {
    var requestOptions, path;
    path = '/api/locations';
    requestOptions = {
        url: apiOptions.server + path,
        method: "GET",
        json: {},
        qs : {
            lng : -117.929835,
            lat : 33.614675,
            max : 30000
        }
    };
    request(requestOptions, function(err, res, body) {
        renderHomepage(req, res, body);
});    

var renderHomepage = function(req, res, body) {
    res.render('jade-template', {
        title: 'I'm a main title',
        pageHeader: {
            title: 'I'm a title',
            strapline: 'I'm a strapline'
        },
        locations: body,
        sidebar: 'yadda yadda'
    });
};

The homelist function gets called from my routes.js which is then where my API gets called from my request with the query found in requestOptions.

however when the request callback is fired and renderHomepage in invoked with the API variable body I get the error:

res.render('jade-template', {
     ^
TypeError: res.render is not a function 

All my routes are set up and tested fine. Does anyone have a solution for this?

Upvotes: 0

Views: 1015

Answers (1)

Gilad Artzi
Gilad Artzi

Reputation: 3084

The res argument in the renderHomepage function (the request's callback) is not the same as the res of the express route!

Change your call the request to something like:

request(requestOptions, function(err, serverRes, body) {
    renderHomepage(req, res, body);
});

Then the response to your request to /api/locations is serverRes, and the response you send to your client is res.

Upvotes: 2

Related Questions