【发布时间】:2021-06-28 16:03:17
【问题描述】:
当我在要使用的模型上调用 updateOne() 时,我正在使用猫鼬和打字稿
someSchema.post<Query>('updateOne', function(doc: IsomeDoc) {
console.log(this)
}
问题是this 是 Query 类型,如果我抑制打字稿检查器并忽略它,因为它给了我一个错误:
Generic type 'Query<ResultType, DocType, THelpers>' requires between 2 and 3 type arguments.
这是我的架构
const someSchema = new Schema(
{
_id: { type: String, required: true },
status: { type: String },
},
{ timestamps: true }
)
如何在函数内获得this 的正确类型?经过大量搜索后,几乎没有使用带有 typescript 和 mongoose 的 post hook。
什么是ResultType?
编辑:
在看到@gregooroo 的回答后,我可以通过将其设为Query<IsomeDoc, IsomeDoc> 来通过查询,但它没有为我提供该对象的正确类型。
【问题讨论】:
标签: node.js typescript mongoose mongoose-schema