Ben M
Ben M

Reputation: 313

Mongoose '.inlcudes' - Argument of type 'ObjectId' is not assignable to parameter of type '{ type: ObjectId; required: true; }'

I'm in the process of converting my JS node/express & mongoose server over to Typescript, and I have an issue with mongoose's array includes method with 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)))

The line causing this issue is below:

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

The error says that the required property is missing, so I thought maybe it was an issue involving userID possibly being null, so I tried this with no success:

transformedUserID || mongoose.Types.ObjectId()

I'm new to Typescript so am not really sure why this is happening, it worked perfectly fine in plain JS. Any help would be appreciated.

Thanks

Upvotes: 5

Views: 5116

Answers (1)

Ben M
Ben M

Reputation: 313

Worked out what the issue was - I was using mongoose.Schema.Types.ObjectId in my typescript interface for the Buildings model, instead of mongoose.Types.ObjectId.

Upvotes: 10

Related Questions