how do I use res.render with mongoose find function?

how do I use res.render with mongoose find function?

Problem

I have a route that gets all of the clients in mongodb and returns them. When I try to use res.render() from with the mongoose find() callback, I get an error saying that ReferenceError: res is not defined

Here’s the code that works and that I’ve confirmed is returning the clients:

app.get( '/clients', function( request, response ) {
    return ClientModel.find( function( err, clients ) {
        if( !err ) {
            return response.send( clients );
        } else {
            return console.log( err );
        }
    });
});

Here’s the code that I’m trying to use to render the ejs view, while passing it a list of clients:

app.get( '/clients', function( request, response ) {
    return ClientModel.find( function( err, clients ) {
        if( !err ) {
            res.render('clients/clients.ejs', {
              clients: clients
            });
        } else {
            return console.log( err );
        }
    });
});

How can I get this to work?

Problem courtesy of: Scott

Solution

Use response.render instead of res.render.

Solution courtesy of: robertklep

Discussion

Leave a Reply

Your email address will not be published. Required fields are marked *