【问题标题】:Json parse is not rendering data in the front end using Node.js & hbsJson parse 不是在前端使用 Node.js 和 hbs 渲染数据
【发布时间】:2021-12-30 05:34:04
【问题描述】:

服务器端:

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

【问题讨论】:

  • 所以我不能只在 hbs 中渲染和放置一个对象?我将如何去获取它?
  • 是的,事实上你可以。但是您随后需要遍历产品列表。像这样:stackoverflow.com/questions/22696886/…

标签: javascript node.js express hbs


【解决方案1】:

遍历列表修复了问题:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-02
    • 2015-11-20
    • 1970-01-01
    • 2020-06-27
    • 2019-04-06
    • 2013-01-30
    • 2017-04-13
    • 1970-01-01
    相关资源
    最近更新 更多