【发布时间】:2021-10-02 08:53:01
【问题描述】:
不知何故,我无法更新从 MongoDB 获取的 mongoose 对象的属性。我正在尝试遵循这种模式:Mongoose Docs: Document
这是我的代码:
// note: getInstances just returns model.find()
let instances: InstanceDocument[] = await this.instanceService.getInstances();
instances.forEach(async (instance, index) => {
console.log(instance);
let deviceCount = await this.instanceService.getDeviceCount(instance._id);
let elementCount = await this.instanceService.getElementCount(instance._id)
instance.deviceCount = deviceCount;
instance.elementCount = elementCount;
await instance.save();
console.log(deviceCount, elementCount, instance);
})
console.log 为deviceCount 和elementCount 打印正确的值,但实例对象保持不变。它仍然具有数据库中未更新的值。
注意:这不是Unable to add properties to js object 的重复条目,因为我不是要创建新对象并为其赋予属性。
【问题讨论】:
标签: javascript typescript mongodb mongoose nestjs