gpt4 book ai didi

node.js - Mongoose - 人口 MissingSchemaError

转载 作者:太空宇宙 更新时间:2023-11-03 23:01:34 27 4
gpt4 key购买 nike

我有两个 Mongoose 模型:

1-事件模型:

eventSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
location: {
type: { type: String, default: 'Point', required: true },
coordinates: { type: [Number], required: true },
},
sport: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Sport',
required: true,
},
startDate: {
type: Date,
required: true,
},
description: {
type: String,
required: true,
},
host: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
players: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
}],
}, { timestamps: true });

2-用户模型:

userSchema = new mongoose.Schema({
email: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
required: true,
},
age: {
type: Number,
required: true,
},
location: {
type: String,
},
}, { timestamps: true });

在我的 EventService 中,我试图通过 id 查找一个事件并返回 sport hostplayers 已填充字段。前两个字段在填充时不会抛出任何错误并且工作正常,但问题出在 players 字段上。它抛出此错误:

{ MissingSchemaError: Schema hasn't been registered for model "players".
Use mongoose.model(name, schema)
at MissingSchemaError (/Users/elias/Documents/tfg/sporter-app/sporter-api/node_modules/mongoose/lib/error/missingSchema.js:20:11)
at NativeConnection.Connection.model (/Users/elias/Documents/tfg/sporter-app/sporter-api/node_modules/mongoose/lib/connection.js:1043:11)
at getModelsMapForPopulate (/Users/elias/Documents/tfg/sporter-app/sporter-api/node_modules/mongoose/lib/model.js:3569:20)
at populate (/Users/elias/Documents/tfg/sporter-app/sporter-api/node_modules/mongoose/lib/model.js:3113:15)
at _populate (/Users/elias/Documents/tfg/sporter-app/sporter-api/node_modules/mongoose/lib/model.js:3081:5)
at Function.Model.populate (/Users/elias/Documents/tfg/sporter-app/sporter-api/node_modules/mongoose/lib/model.js:3041:5)
at Immediate.<anonymous> (/Users/elias/Documents/tfg/sporter-app/sporter-api/node_modules/mongoose/lib/query.js:1441:17)
at Immediate.<anonymous> (/Users/elias/Documents/tfg/sporter-app/sporter-api/node_modules/mquery/lib/utils.js:137:16)
at runCallback (timers.js:781:20)
at tryOnImmediate (timers.js:743:5)
at processImmediate [as _immediateCallback] (timers.js:714:5)
message: 'Schema hasn\'t been registered for model "players".\nUse mongoose.model(name, schema)',
name: 'MissingSchemaError' }

最后,这就是我调用 .populate() 方法的方式:

const event = await Event.findById(eventId)
.populate('sport', 'host', 'players')
.exec();

知道我做错了什么吗?谢谢!

最佳答案

试试这个:

const event = await Event.findById(eventId)
.populate('sport')
.populate('host')
.populate('players')
.exec()

此处的文档中有一个填充多个路径部分: http://mongoosejs.com/docs/populate.html

关于node.js - Mongoose - 人口 MissingSchemaError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46815344/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com