1

I'm building a bookstore app am using an SQL database with the mysqljs module i can fetch all books from database but i want to access one book and display its details.

I fetch the book object by its id and i display it in the console

book details route:

/* GET Book Details page. */
router.get('/details/:id', function(req, res, next) {
    var sql = `SELECT * FROM books WHERE id = ${req.params.id}`;
    var query = db.query(sql, function (err, result) {
        if(err) throw err;
        console.log(result);
        var model = {result: result}
        res.render('shop/details', { title: 'Books', model });
    });
});

displays this on the console

this is what the console displays

in my handlebars code i wrote

<h1 class="text-center">Book Details Here</h1>
<h3> Your Book Name is {{this.BookName}}</h3>

but it only displays "Your Book Name is " without the book name

Baraa Araki
  • 61
  • 1
  • 6

1 Answers1

0

It looks like your template, is expecting a Book object as a context and in your sample code you are passing it a result key which is mapped into an array containing the book object.

Here is how I usually debug such issues: I would pass an object literal as a context just to make sure the render method works as I expect.

res.render('shop/details', { title: 'Books', {BookName: 'Season of migration to the north',id: 198} });

if this work, then you need to change the model that you pass to the page. so instead you will be passing request[0];

if this does not work, I would log the entire context object to the template to debug it. this post should help you how to do so: Display Handlebars.js context in a template

Good Luck!