对于 MongoDB 的模式验证器,我有以下内容:{
UserSchema.path('email').validate(async function (email: string) {
const count = await User.count({ email, _id: { $ne: this._id } })
return !count
}, 'Email already exists')
我收到以下错误:'this' implicitly has type 'any' because it does not have a type annotation.ts(2683)
User.ts(171, 35): An outer value of 'this' is shadowed by this container.
这是在我的 User.ts
中定义的文件。一切都按预期工作,但是这个 Typescript 错误阻止了 CI 继续。有没有办法解决这个问题(没有双关语)。
最佳答案
尝试:
UserSchema.path('email').validate(async function (this:any, email: string) {
const count = await User.count({ email, _id: { $ne: this._id } })
return !count
}, 'Email already exists')
您可以使用您的类型而不是“任何”。以及文档链接:https://www.typescriptlang.org/docs/handbook/functions.html#this-parameters
关于mongodb - 'this' 的外部值被此容器与 Mongoose Schema Typescript 遮蔽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66168628/