我正在将我的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)) {
...该错误说明缺少required属性,所以我认为这可能是涉及userID可能为空的问题,所以我尝试了一下,但没有成功:
transformedUserID || mongoose.Types.ObjectId()我刚开始使用Typescript,所以我不太确定为什么会发生这种情况,它在普通的JS中工作得很好。任何帮助都将不胜感激。
谢谢
发布于 2020-07-02 16:13:35
解决了这个问题-我在我的建筑模型的typescript界面中使用了mongoose.Schema.Types.ObjectId,而不是mongoose.Types.ObjectId。
https://stackoverflow.com/questions/62685996
复制相似问题