嘗試使用 .findOne 方法查找 mongoDB 條目,但它回傳以下錯誤:
TypeError: Cannot read properties of undefined (reading 'findOne')
我覺得這與我的模式檔案中的 .models/.model 方法有關,因為我沒有收到任何編譯資訊,即回傳undefined型別。我可能做錯了什么,所以任何幫助指出它將不勝感激 - 謝謝
架構檔案:
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const banSchema = new Schema({
userId: {
type: Number,
required: true
},
reason: {
type: String,
required: true
},
period: {
type: String,
required: false,
default: 'permanent'
}
})
const name = 'Ban'
//
console.log(typeof(mongoose.models[name] || mongoose.model[name, banSchema])) // Undefined
//
module.exports = mongoose.models[name] || mongoose.model[name, banSchema]
uj5u.com熱心網友回復:
你應該像這樣匯出你的模型
module.exports = mongoose.model('Ban', banSchema);
你可以在這里閱讀更多關于它的資訊
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/503992.html
