【发布时间】:2020-07-02 16:13:35
【问题描述】:
我正在将我的 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)) {
...
错误说缺少必需的属性,所以我认为这可能是一个涉及 userID 可能为 null 的问题,所以我尝试了这个但没有成功:
transformedUserID || mongoose.Types.ObjectId()
我是 Typescript 的新手,所以我不确定为什么会这样,它在纯 JS 中运行得非常好。任何帮助将不胜感激。
谢谢
【问题讨论】:
标签: node.js typescript mongoose