我的Node应用程序中相关的表达部分:
/*Route to Product Views*/
app.get('/product/:id', function(req, res){
Product.find({_id: req.params.id}, function (error, data) {
if(error){
console.log(error);
} else {
console.log("DATA :" + data); //correct json object
res.render('product',{
title: 'Product Template',
result: data
}
);
}
});
});
Jade模板:
!!! 5
html
head
title #{title}
body
h1 #{result.name}
h2 #{result.unitprice}
p.
#{result.description}
h3 #{result}
因此,如果我访问http://myhost.com/product/51fa8402803244fb12000001,我只会看到
#{result}
的输出结果。[{
__v: 0,
_id: 51fa8402803244fb12000001,
description: 'Awesome stuff you really need',
discontinued: false,
name: 'Some product',
unitprice: 5.99
}]
使用JSON.stringify没有什么不同,只是h3 #{result}返回“字符串化”的JSON。 如何正确地访问json字符串的字段?
result
是一个单文档数组,你不应该使用result[0].name
、result[0].unitprice
吗?console.log
打印了什么? - c.P.u1