1

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

  • So I can't just render and put an object in the hbs? How would I go about fetching it? –  Nov 19 '21 at 12:52
  • Yes, in fact you can. But you then need to iterate through the list of products. Like so: https://stackoverflow.com/questions/22696886/how-to-iterate-over-array-of-objects-in-handlebars – mutantkeyboard Nov 19 '21 at 13:03

1 Answers1

0

Iterating through the list fixed the issue:

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