首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mongoose '.inlcudes‘-类型为'ObjectId’的参数不能赋给类型为'{ type: ObjectId;required: true;}‘的参数

Mongoose '.inlcudes‘-类型为'ObjectId’的参数不能赋给类型为'{ type: ObjectId;required: true;}‘的参数
EN

Stack Overflow用户
提问于 2020-07-02 06:03:30
回答 1查看 1K关注 0票数 0

我正在将我的JS节点/express& mongoose服务器转换为Typescript,我对mongoose的数组包含方法与typescript有一个问题:

代码语言:javascript
复制
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)))

导致此问题的代码行如下:

代码语言:javascript
复制
const transformedUserID = mongoose.Types.ObjectId(userID)
if (transformedUserID && !building.actors.includes(transformedUserID)) {
   ...

该错误说明缺少required属性,所以我认为这可能是涉及userID可能为空的问题,所以我尝试了一下,但没有成功:

代码语言:javascript
复制
transformedUserID || mongoose.Types.ObjectId()

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

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-02 16:13:35

解决了这个问题-我在我的建筑模型的typescript界面中使用了mongoose.Schema.Types.ObjectId,而不是mongoose.Types.ObjectId

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62685996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档