【问题标题】:Mongoose '.inlcudes' - Argument of type 'ObjectId' is not assignable to parameter of type '{ type: ObjectId; required: true; }'Mongoose '.inlcudes' - 'ObjectId' 类型的参数不可分配给'{ type: ObjectId; 类型的参数;要求:真; }'
【发布时间】:2020-07-02 16:13:35
【问题描述】:

我正在将我的 JS 节点/express 和 mongoose 服务器转换为 Typescript,我对 mongoose 的数组包含 typescript 的方法有疑问:

Argument of type 'ObjectId' is not assignable to parameter of type '{ type: ObjectId; required: true; }'.
  Type 'ObjectId' is missing the following properties from type '{ type: ObjectId; required: true; }': type, required

78     if (!building.actors.includes(mongoose.Types.ObjectId(userID)))

导致此问题的行如下:

const transformedUserID = mongoose.Types.ObjectId(userID)
if (transformedUserID && !building.actors.includes(transformedUserID)) {
   ...

错误说缺少必需的属性,所以我认为这可能是一个涉及 userID 可能为 null 的问题,所以我尝试了这个但没有成功:

transformedUserID || mongoose.Types.ObjectId()

我是 Typescript 的新手,所以我不确定为什么会这样,它在纯 JS 中运行得非常好。任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: node.js typescript mongoose


    【解决方案1】:

    找出问题所在 - 我在建筑模型的打字稿界面中使用了mongoose.Schema.Types.ObjectId,而不是mongoose.Types.ObjectId

    【讨论】:

      猜你喜欢
      • 2019-02-08
      • 1970-01-01
      • 2021-11-23
      • 2020-07-27
      • 2020-08-04
      • 2019-06-24
      • 2021-06-14
      • 2022-06-29
      相关资源
      最近更新 更多