Mongodb 外部值为';这';使用Mongoose架构Typescript对此容器进行跟踪

Mongodb 外部值为';这';使用Mongoose架构Typescript对此容器进行跟踪,mongodb,typescript,mongoose,Mongodb,Typescript,Mongoose,对于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' beca

对于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')
您可以使用您的类型而不是“any”


指向docu的链接:

这很有效!在您回答之前,我实际上使用了一个插件,该插件更改了默认的
unique
字段行为,将其显示为正常的验证错误,以便我可以将验证错误消息直接放入模式本身。