Mongoose '.inlcudes' - 'ObjectId' 类型的参数不可分配给 '{ type: ObjectId; 类型的参数;必需:真实;}'

Ben*_*GHT 4 mongoose node.js typescript

我正在将 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)))
Run Code Online (Sandbox Code Playgroud)

导致此问题的行如下:

const transformedUserID = mongoose.Types.ObjectId(userID)
if (transformedUserID && !building.actors.includes(transformedUserID)) {
   ...
Run Code Online (Sandbox Code Playgroud)

该错误表明缺少所需的属性,因此我认为这可能是涉及 userID 可能为空的问题,所以我尝试了此操作但没有成功:

transformedUserID || mongoose.Types.ObjectId()
Run Code Online (Sandbox Code Playgroud)

我是 Typescript 的新手,所以不太确定为什么会发生这种情况,它在普通 JS 中工作得很好。任何帮助,将不胜感激。

谢谢

Ben*_*GHT 9

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