无法在 NodeJS 服务工作者中填充猫鼬模式模型

时间:2021-04-02 11:01:17

标签: node.js mongodb express mongoose service-worker

我正在研究 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,
        required: true
    },
    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,
        required: true
    },
    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

感谢任何帮助, 提前致谢!

0 个答案:

没有答案
相关问题