mongodb - 'this' 的外部值被此容器与 Mongoose Schema Typescript 遮蔽

标签 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' 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/

相关文章:

node.js - MongoDB:计算集合中文档出现的次数

java - 获取字符串值的最大值

mongodb - 分片集合中 mongodb 中的聚合管道

mongodb - 为什么 MongoLab 不推荐使用他们的 REST API?

即使只进行了插入,mongodb 内存使用率也会很高

javascript - 将 Array.prototype.concat.apply([], [x]) 重构为 [].concat(x) 会引发错误

javascript - Express pre remove 触发另一个 pre remove on remove

javascript - Angular 2 json获取数据对象

angular - 错误错误 : Uncaught (in promise): invalid link: LoginPage

node.js - MongoDB 和 Nodejs 与 Docker "MongoTimeoutError: Server selection timed out after 30000 ms"