Enzo Barrera
Enzo Barrera

Reputation: 455

Why is res.render() not working with req.params.id?

I have been trying to make this work on this project about showing dogs for adoption, every other route works perfectly but this one /dog/:id which requires an id from my mongoDB database is not working with res.render().

res.render() is working on other routes without req.params.id

As you can see in the code below the line with this method is commented out and this is the line I want to make work; res.send() works perfectly but I don't know why res.render() does not. Thanks!!!

router.route('/dog/:id')
.get(async (req, res) => {
    const id = req.params.id
    const adoption = await Adoption.findById(id)
        .populate('category');
    const categories = await Category.find();

    /* res.render('default/dog', { adoption: adoption, categories: categories }); */
    res.send(JSON.stringify(adoption));
});

By the way, I'm using node.js with express and handlebars for rendering different views for each route.

Upvotes: 1

Views: 1648

Answers (3)

Enzo Barrera
Enzo Barrera

Reputation: 455

Ok so after trying to figure out why the hell this was not working I spot the error... and it was not from the backend it was a script tag that was not loading correctly ._. so it was just frontend stuff:

<script src="js/revolution.extension.actions.min.js"></script>

When not using a route with additional params it works fine but if you want to make it work on every route (I mean, simple ones and the ones with params) you have to include / at the very beginning, like this:

<script src="/js/revolution.extension.actions.min.js"></script>

And it worked!!! 🥳

Upvotes: 4

Usman Abdur Rehman
Usman Abdur Rehman

Reputation: 464

res.render doesn't work alone or are you trying to render the template and send the response at the same time? If that is the case than the template will be rendered first but then the json response will be sent so it would look like express is not rendering the template.

If that's not the case and you are only running res.render than check the path default/dog. Is there a directory default inside views inside which you have got the dog handlebars template. Check that also and then tell if it solves your problem

Upvotes: 0

C.Gochev
C.Gochev

Reputation: 1922

I assume you have a views directory and folder structure something like that:

- routes
    | -- index.js
-config
    |-- config.js
- client
    |-- public
    |     | -- js
    |     | -- css
    |     | -- font
- views
    | -- dog.handlebars
    | -- layout
           | -- main.handlebars
- server.js

And in server.js you probably initialize handlebars like this:

 const hbs = exphbs.create({
        defaultLayout: 'main.handlebars',
        helpers: {....}
        })
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');

If this is the case and you do it like that you can simply call res.render with 'dog' like this:

res.render('dog', { adoption: adoption, categories: categories });

Upvotes: 0

Related Questions