简体   繁体   English

无法读取未定义的属性-NodeJS,Express,Mongoose

[英]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.

相关问题 无法读取未定义的猫鼬Node.js的属性'id' - Cannot read property 'id' of undefined mongoose Nodejs Express 和 Mongoose:无法读取 Postman 中未定义的属性“名称” - Express and Mongoose: Cannot read property 'name' of undefined in Postman 类型错误:无法读取未定义的 nodejs express mvc 的属性“render” - TypeError: Cannot read property 'render' of undefined nodejs express mvc NodeJS / Express中的setTimeout抛出TypeError:无法读取未定义的属性'client' - setTimeout in NodeJS /Express throws TypeError: Cannot read property 'client' of undefined NodeJS Express 模块错误:TypeError:无法读取未定义的属性“已完成” - NodeJS Express Module Error: TypeError: Cannot read property 'finished' of undefined 无法读取Node.js中未定义的属性'then' - Cannot read property 'then' of undefined in nodejs Mongoose - 无法读取未定义的属性“推送” - Mongoose - Cannot read property 'push' of undefined Mongoose 模式无法读取未定义的属性“密码” - Mongoose schema Cannot read property 'password' of undefined 猫鼬:TypeError:无法读取未定义的属性“ findOne” - Mongoose: TypeError: Cannot read property 'findOne' of undefined 类型错误:无法使用猫鼬读取未定义的属性“查找” - TypeError: Cannot read property 'find' of undefined with mongoose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM