[英]Cannot read property of undefined - NodeJS, Express, Mongoose
I got this two functions 我有这两个功能
exports.list = function (req, res){
Material.find(function(err, materials) {
res.render('materials/list', {title: 'Pagina Materiali', materials: materials});
});
}
exports.modify = function (req, res){
Material.findById(req.params.id, function(err, materials){
res.render('materials/edit', {title: 'Pagina materiale singolo', materials: materials});
});
}
The first one works properly and I cycle it with this Jade snippet: 第一个正常工作,我用这个Jade片段循环它:
each material in materials
p Nome materiale: #{material.m_name} | Tipo materiale: #{material.type} |
a(href='materials/edit/#{material.id}') Modifica Materiale
But I can't get the second one to work and Express tell me that it cannot read properties of an undefined. 但是我无法使用第二个,Express告诉我它无法读取未定义的属性。 If I view the result in the console or just return it I can see that it's picking the right value, it's just that I'm not able to show it properly in the template. 如果我在控制台中查看结果或只是返回结果,我可以看到它选择了正确的值,只是我无法在模板中正确显示它。 Any help? 有什么帮助吗? Thanks! 谢谢!
findById finds a single document by id. findById通过ID查找单个文档。 You trying to each it as array of objects. 您尝试将其作为对象数组。
You need something like this in node: 您需要在节点中执行以下操作:
Material.findById(req.params.id, function(err, material){
res.render('materials/edit', {title: 'Pagina materiale singolo', material: material});
});
In jade: 在玉:
p Nome materiale: #{material.m_name} | Tipo materiale: #{material.type} |
a(href='materials/edit/#{material.id}') Modifica Materiale
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.