【发布时间】:2013-08-03 16:50:50
【问题描述】:
我的节点应用程序的相关 Express 部分:
/*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
}
);
}
});
});
翡翠模板:
!!! 5
html
head
title #{title}
body
h1 #{result.name}
h2 #{result.unitprice}
p.
#{result.description}
h3 #{result}
因此,如果我访问http://myhost.com/product/51fa8402803244fb12000001,我看到的只是 h3 #{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打印什么? -
谢谢兄弟,如果 JSON.stringify() 不 被使用,它的工作原理就是这样!