首页 > 解决方案 > Mongoose '.inlcudes' - 'ObjectId' 类型的参数不可分配给'{ type: ObjectId; 类型的参数;要求:真;}'

问题描述

我正在将我的 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)) {
   ...

该错误表示缺少所需的属性,所以我认为这可能是一个涉及用户 ID 可能为空的问题,所以我尝试了这个但没有成功:

transformedUserID || mongoose.Types.ObjectId()

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

谢谢

标签: node.jstypescriptmongoose

解决方案


解决了问题所在 - 我mongoose.Schema.Types.ObjectId在我的打字稿界面中使用了 Buildings 模型,而不是mongoose.Types.ObjectId.


推荐阅读