Sunday, 12 March 2017

How to pass url parameter to the template in express js

I have the following code in my server.js:

app.get('/animal/:id', function (request, response)
{
    Animal.find({_id: request.params.id}, function(err, animals)
    {
        response.render('animal', { animals: animals });
    })
})

I am trying to get the animal id to show up in the template url.

           <table border='1'>
                <tr>
                    <th>Name</th>
                    <th>Actions</th>
                </tr>
            <% for (var i in animals){ %>
                    <tr>
                        <td><a href='/animal/?id=**ID RIGHT HERE** %>><%= animals[i].name %></a></td>
                        <td><a href='#'>Edit</a> <a href='#'>Delete</a></td>
                    </tr>

            <% } %>
            </table>  

If I just put in animals[i]._id, it treats as a string, but if I write it as <%= animals[i]._id %>, the whole column is removed.



via JSim

No comments:

Post a Comment