我創建了兩個模型,我試圖將一個模型參考到另一個模型,但它不起作用,我對此一無所知我使用它的方式與貓鼬示例中的完全相同,但它沒有幫助,唯一的區別在我的作業和他們的例子之間是我有兩個不同的模型檔案,我正在匯出它們以在另一個檔案中使用
我從貓鼬那里參考的例子
第一個模型
const userModel = require('./userModel')
const {Schema} = mongoose
const tweet = new Schema({
date: {
type: Date,
required: true,
},
msg:{
type: String,
maxlength:140,
required:true
},
tweetid:{
type: mongoose.Types.ObjectId
}
})
const tweetSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'userModel'
},
tweets: {
type: [{tweet}],
default: []
}
})
const tweetModel = mongoose.model('tweet',tweetSchema)
module.exports = tweetModel
第二個模型
const {Schema} = mongoose
const userSchema = new Schema({
email: {
type:String,
required: true,
unique: true,
trim:true
},
password: {
type:String,
required: true,
trim:true
},
follows: {
type: [String],
default: []
}
})
const userModel = mongoose.model("User",userSchema)
module.exports = userModel
uj5u.com熱心網友回復:
您的參考字串應等于您傳遞給的模型名稱mongoose.model:
const tweetSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'User'
},
tweets: {
type: [{tweet}],
default: []
}
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/399737.html
上一篇:NodeJS函式,它接受一個無符號整數并回傳錯誤數量的“1”位
下一篇:依賴模式也應該編譯嗎?
