node.js - 无法在 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,
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
任何帮助表示赞赏,在此先感谢!
解决方案
推荐阅读
- android - 由于缺少权限,无法加载 Android Facebook 登录
- python - 使用 Python 3 从 URL 中提取图像
- php - 通过 php 到 gmail 的 html 电子邮件与消息中的文本不兼容
- python-3.x - 如何小写python中每个新行的第一个字母?
- python - 如何将 2d 列表转换为 2d 本机 python 数组而不是 numpy 数组?
- ios - 如何组合仅样式化分段控件样式的重复代码?
- dialog - 模拟 Vaadin 8 窗口标题的 Vaadin 10 对话框
- c - 如何从 C 中的完整路径(字符串)将文本附加到文件名?
- python - 我希望 Django orm 从 dB 表中找到当前日期和日期时间字段之间的天数差异
- android - Android:使用迭代器解析 JSONArray 键