SKe*_*ney 4 mongoose mongodb typescript
对于 MongoDB 的模式验证器,我有以下内容:{
UserSchema.path('email').validate(async function (email: string) {
const count = await User.count({ email, _id: { $ne: this._id } })
return !count
}, 'Email already exists')
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
'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.
Run Code Online (Sandbox Code Playgroud)
这是在我的User.ts文件中定义的。一切都按预期工作,但是这个 Typescript 错误阻止了 CI 继续。有没有办法解决这个问题(没有双关语)。
小智 6
尝试:
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')
Run Code Online (Sandbox Code Playgroud)
您可以使用您的类型而不是“任何”。
以及文档的链接:https : //www.typescriptlang.org/docs/handbook/functions.html#this-parameters
| 归档时间: |
|
| 查看次数: |
1021 次 |
| 最近记录: |