Json parse 不会使用 Node.js 和 hbs 在前端渲染数据



服务器端:

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在前端显示价格,但没有显示

遍历列表修复了问题:

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

{{/products}}

最新更新