How can I use url query string when working with ejs

问题内容:

Is there a way to pass a query string inside a href in ejs? I want to be able to click on a link and use the the link parameter inside my db query. Here’s the code I’m playing with at the moment

//ejs view
<% if (data) { %>
<% data.forEach(function(car) { %>
    <div class="card mb-3">
        <a href="/cars/:id">
            <div class="card-body text-center text-capitalize">
                <div class="card-title"><%= car.brand %></div>
                <p class="card-text text-muted"><%= car.color %> / <%= car.engine_type %></p>
            </div>
        </a>
    </div>
<% }) %>
<% } %>

In my routes file, I want to query like this

router.get('/cars/:id?', function(req, res) {
var carId = req.params.id;

res.locals.connection.query('SELECT * FROM cars WHERE id = ?', [carId], function(err, row) {
    if(err) {
        console.log(err);
    } else {
        res.send(row);
    }
});
});

Right now i get a 304 status when i run my app because i’m not passing back the id. How can i pass it back? I have no idea and haven’t been successful with my search.

问题评论:

原文地址:

https://stackoverflow.com/questions/47754496/how-can-i-use-url-query-string-when-working-with-ejs

添加评论

友情链接:蝴蝶教程