Node.js 猫鼬;。包括';-类型为'的参数;ObjectId';不可分配给类型为';{type:ObjectId;必需:true;}';
我正在将我的JS node/express&mongoose服务器转换为Typescript,我对mongoose的数组包含方法和Typescript有一个问题:Node.js 猫鼬;。包括';-类型为'的参数;ObjectId';不可分配给类型为';{type:ObjectId;必需:true;}';,node.js,typescript,mongoose,Node.js,Typescript,Mongoose,我正在将我的JS node/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 typ
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中工作得非常好。任何帮助都将不胜感激
谢谢解决了问题所在-我在建筑模型的typescript界面中使用了
mongoose.Schema.Types.ObjectId
,而不是mongoose.Types.ObjectId