簡體   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