'this'的外部值被此容器与MongooseSchemaTypescript遮蔽

对于 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


以上是'this'的外部值被此容器与MongooseSchemaTypescript遮蔽的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>