1

服务器端:

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
    }
]

}

前端:

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

我尝试使用 hbs 在前端显示价格,但没有显示任何内容

4

1 回答 1

0

遍历列表修复了问题:

{{#products}}
<small>Price: £ {{price}}</small>
                   
{{/products}}
于 2021-11-19T13:57:26.510 回答