node.js - Mongoose '.inlcudes' - 'ObjectId' 类型的参数不可分配给'{ type: ObjectId; 类型的参数;要求:真;}'
问题描述
我正在将我的 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)) {
...
该错误表示缺少所需的属性,所以我认为这可能是一个涉及用户 ID 可能为空的问题,所以我尝试了这个但没有成功:
transformedUserID || mongoose.Types.ObjectId()
我是 Typescript 的新手,所以不太确定为什么会这样,它在纯 JS 中运行得非常好。任何帮助,将不胜感激。
谢谢
解决方案
解决了问题所在 - 我mongoose.Schema.Types.ObjectId
在我的打字稿界面中使用了 Buildings 模型,而不是mongoose.Types.ObjectId
.
推荐阅读
- python-dateutil - 导入包含其中一个字段的excel为空时如何修复引发ValueError(“未知字符串格式:”,timestr)
- javascript - 如何在网页上使用拖放将文件保存到磁盘
- java - 使用 Google Photo API android 在 android 中共享和导入图像
- html - 简单的本地网页链接到本地 pdf 文件
- android - 动态功能使用外部 aar 依赖项时如何修复“程序类型已存在”错误
- python - 除了自变量之外,如何为 scipy.optimize.minimize 的目标函数提供额外的输入
- azure-pipelines - 在 azure datafactory 管道中一一提供 azure databrick 活动中的文件名
- python - 在迭代时更改 JSON 结构
- excel-formula - 如何在 Power Pivot 中对两个查找值结果求和
- angular - 我可以在我的计算机上使用多个角度版本吗?