繁体   English   中英

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

[英]mongoose through mongo atlas TypeError: Cannot read property 'length' of undefined

我正在使用 mongoose,我在从本地 mongodb 服务器(mongod)移动到 mongodb atlas cloud 后遇到了这个问题:TypeError:无法读取属性“长度”未定义

这是导致问题的获取代码片段

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
    }

});

});

这是 github 中的完整代码:

https://github.com/oubaydos/toDoList-webDevLearningPath/blob/main/app.js

错误的根本原因是您声明的架构根本不是真正的架构。 您需要将其声明为 mongoose 架构,所以而不是

const itemsSchema = {
    name: String
};

你应该做:

const itemSchema = new mongoose.Schema({
    name: String
});

参考: mongoose.js 文档

多亏了 ISAE,我发现问题出在与 mongodb 地图集数据库的连接上,而我与本地主机建立了连接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM