[英]mongoose through mongo atlas TypeError: Cannot read property 'length' of undefined
i am using mongoose and i encountered this problem after moving from the local mongodb server (mongod) to mongodb atlas cloud: TypeError: Cannot read property 'length' of undefined我正在使用 mongoose,我在从本地 mongodb 服务器(mongod)移动到 mongodb atlas cloud 后遇到了这个问题:TypeError:无法读取属性“长度”未定义
here's a snippet of the get code causing the problem这是导致问题的获取代码片段
app.get('/', function(req, res) {
Item.find({}, function(err, results) {
//Item is a mongoose model
if (results.length === 0) {
item.save();
res.redirect("/");
} else {
res.render('toDoLists', { Title: title, addedItems: results });
//using ejs
}
});
}); });
here's the whole code in github:这是 github 中的完整代码:
https://github.com/oubaydos/toDoList-webDevLearningPath/blob/main/app.js https://github.com/oubaydos/toDoList-webDevLearningPath/blob/main/app.js
The underlying cause for the error is that your declared schema isn't really a schema at all.错误的根本原因是您声明的架构根本不是真正的架构。 You need to declare it as a mongoose schema, so instead of
您需要将其声明为 mongoose 架构,所以而不是
const itemsSchema = {
name: String
};
You should do:你应该做:
const itemSchema = new mongoose.Schema({
name: String
});
reference: mongoose.js docs参考: mongoose.js 文档
thanks to ISAE i found that the problem was in the connection with the mongodb atlas database, i had a connection with the localhost instead.多亏了 ISAE,我发现问题出在与 mongodb 地图集数据库的连接上,而我与本地主机建立了连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.