如何解决无法在 NodeJS 服务工作者中填充猫鼬模式模型
我正在研究 MERN Stack 设置以及 Mongoose 和 Typescript。
我正在尝试在 NodeJS Service Worker 上运行一个简单的查询,以查找单个模型文档并填充其参考文档(标签)。
然而,这个查询在 Express 控制器中运行得非常好,但在 Service Worker 上却没有。
这是我的代码:
架构模型
/**
* Question Model
*/
export interface QuestionModel extends QuestionRecord,Document {}
export const QuestionSchema: Schema = new Schema({
title: {
type: String,required: true
},type: {
type: String,description: String,tags: [{
type: Schema.Types.ObjectId,ref: 'Tag'
}],isDraft: {
type: Boolean,default: true
}
},{
timestamps: true
})
/**
* Tag Model
*/
export interface TagModel extends TagRecord,Document {}
export const TagSchema: Schema = new Schema({
key: {
type: String,name: {
type: String,required: true
}
},{
timestamps: true
})
查询(在 Service Worker 中)
const question = await QuestionSchemaModel.findById(questionId,{ title: 1,tags: 1,type: 1,description: 1 }).populate('tags');
console.log('Print Tags:',question.tags);
my-api | Worker for job "requeueExpired" had an error {
my-api | err: Error [MissingSchemaError]: Schema hasn't been registered for model "Tag".
my-api | Use mongoose.model(name,schema)
my-api | at NativeConnection.Connection.model (/usr/src/app/node_modules/mongoose/lib/connection.js:1255:11)
my-api | at getModelsMapForPopulate (/usr/src/app/node_modules/mongoose/lib/helpers/populate/getModelsMapForPopulate.js:301:59)
my-api | at populate (/usr/src/app/node_modules/mongoose/lib/model.js:4338:21)
my-api | at _populate (/usr/src/app/node_modules/mongoose/lib/model.js:4308:5)
my-api | at /usr/src/app/node_modules/mongoose/lib/model.js:4284:5
my-api | at promiSEOrCallback (/usr/src/app/node_modules/mongoose/lib/helpers/promiSEOrCallback.js:9:12)
my-api | at Mongoose._promiSEOrCallback (/usr/src/app/node_modules/mongoose/lib/index.js:1135:10)
my-api | at Function.Model.populate (/usr/src/app/node_modules/mongoose/lib/model.js:4282:23)
my-api | at model.Query.Query._completeOne (/usr/src/app/node_modules/mongoose/lib/query.js:2078:9)
my-api | at Immediate.<anonymous> (/usr/src/app/node_modules/mongoose/lib/query.js:2117:10)
my-api | at Immediate.<anonymous> (/usr/src/app/node_modules/mquery/lib/utils.js:124:16)
my-api | at processImmediate (node:internal/timers:463:21)
my-api | }
my-api | Worker for job "requeueExpired" exited with code 1 undefined
感谢任何帮助, 提前致谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。