简体   繁体   English

mongoose 通过 mongo atlas TypeError:无法读取未定义的属性“长度”

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

相关问题 Mongoose TypeError:无法读取undefined的属性'googleID' - Mongoose TypeError: Cannot read property 'googleID' of undefined 猫鼬:TypeError:无法读取未定义的属性“ findOne” - Mongoose: TypeError: Cannot read property 'findOne' of undefined 类型错误:无法使用猫鼬读取未定义的属性“查找” - TypeError: Cannot read property 'find' of undefined with mongoose 未捕获的TypeError:无法读取未定义的属性“长度” - Uncaught TypeError: Cannot read property 'length' of undefined TypeError无法读取未定义的属性“ length”-角度4 - TypeError cannot read property “length” of undefined - angular 4 opencv-TypeError:无法读取未定义的属性“长度” - opencv - TypeError: Cannot read property 'length' of undefined 未捕获的TypeError:无法读取未定义的属性“ length” - Uncaught TypeError: Cannot read property 'length' of undefined UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“长度” - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of undefined 未捕获的TypeError:无法读取未定义的属性“ length” - Uncaught TypeError: Cannot read property 'length' of undefined 开玩笑:TypeError:无法读取未定义的属性“长度” - Jest : TypeError: Cannot read property 'length' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM