javascriptnode.jsexpresshbs

Json parse is not rendering data in the front end using Node.js & hbs


Server side:

app.get("/basket", (req, res) => {
fs.readFile("products.json", (err, data) => {
if (err) {
  res.status(500).end()
} else {
  res.render("basket", {products: JSON.parse(data)})
}
})
})

JSON:

{
"product_one": [
    {
        "name": "Laptop",
        "price": 799
    }
],

"product_two": [
    {
        "name": "Laptop",
        "price": 799
    }
]

}

Front-end:

                             <small>Price: £{{products.product_one.price}} </small>

I have tried to display the price in the front-end using hbs but nothing is displaying


Solution

  • Iterating through the list fixed the issue:

    {{#products}}
    <small>Price: £ {{price}}</small>
                       
    {{/products}}