【发布时间】:2021-12-31 14:55:07
【问题描述】:
我创建了两个模型,我试图将一个模型引用到另一个模型,但它不起作用,我对此一无所知。我使用它的方式与猫鼬示例中的完全相同,但它没有帮助,我的工作和他们的示例之间的唯一区别是我对两个模型都有两个不同的文件,我将它们导出以在另一个文件中使用
我参考Mongoose的例子
第一个模型
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
【问题讨论】: