javascript - mongoose 通过 mongo atlas TypeError:无法读取未定义的属性“长度”
问题描述
我正在使用 mongoose,在从本地 mongodb 服务器(mongod)移动到 mongodb atlas cloud 后遇到了这个问题:TypeError: Cannot read property 'length' of undefined
这是导致问题的获取代码片段
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
解决方案
错误的根本原因是您声明的架构根本不是真正的架构。您需要将其声明为猫鼬模式,因此而不是
const itemsSchema = {
name: String
};
你应该做:
const itemSchema = new mongoose.Schema({
name: String
});
推荐阅读
- django - django fetching wrong datetime from database (inconsistent time on DateTimeField)
- macos - 在 Electron JS 上恢复主窗口
- javascript - 如何将搜索结果存储在反应组件中以供将来使用
- python - 按等级对加权值进行分组和排序
- reactjs - 在状态中检测到不可序列化的值
- reactjs - 开玩笑地在@auth0 中模拟 Auth0Client
- android - 带有 firebaseUI 的 Twitter Auth 不起作用。尝试获取包证书哈希时出错
- python - 基于另一个表中的相似性从同一个表更新 SQL
- angularjs - 我的 app.module 提供程序中的多个初始化程序导致我的初始视图不显示
- python-3.x - 为什么 JSON 数据不会进入 Flask 服务器?