简体   繁体   English

无法使用 nestjs 更新猫鼬对象中的属性

[英]Unable to update properties in mongoose object using nestjs

Somehow I'm unable to update properties of a mongoose object I fetch from a MongoDB.不知何故,我无法更新从 MongoDB 获取的猫鼬对象的属性。 I'm trying to follow this pattern: Mongoose Docs: Document我正在尝试遵循这种模式: Mongoose Docs: Document

This is my code:这是我的代码:

// 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);
})

The console.log prints the correct values for deviceCount and elementCount , but the instance object remains unmodified. console.log打印deviceCountelementCount的正确值,但实例对象保持不变。 It still has the unupdated values it has in the database.它仍然具有数据库中未更新的值。

Note: this is not a duplicate entry of Unable to add properties to js object , as I'm not trying to create a new object and give it properties.注意:这不是Unable to add properties to js object的重复条目,因为我没有尝试创建新对象并为其提供属性。

Two things :两件事情 :

  1. You can't use await inside an array method like forEach or map .您不能在诸如forEachmap类的数组方法中使用await It doesn't work (doesn't await).不起作用(不等待)。 Use a for loop instead.改用for循环。

  2. Mongoose has this weird requirement that you must explicitely tell it that a nested key has been modified in order to save it. Mongoose 有一个奇怪的要求,你必须明确地告诉它一个嵌套的键已经被修改以保存它。 See this question看到这个问题

let instances: InstanceDocument[] = await this.instanceService.getInstances();

for(let instance of instances) {
    console.log(instance);
    instance.deviceCount = await this.instanceService.getDeviceCount(instance._id);
    instance.elementCount = await this.instanceService.getElementCount(instance._id);

    instance.markModified("deviceCount"); // this
    instance.markModified("elementCount"); // and this

    await instance.save();
    console.log(deviceCount, elementCount, instance);
}

The code above works.上面的代码有效。 I made a mistake in defining the objects schema.我在定义对象模式时犯了一个错误。 I missed @Prop() decorator for the properties I added.我错过了我添加的属性的 @Prop() 装饰器。 This code works:此代码有效:

let instances: InstanceDocument[] = await this.instanceService.getInstances();
let fetchingDone = new Subject();
fetchingDone.subscribe(instances => res.json(instances))

instances.forEach(async (instance, index) => {
  instance.deviceCount = await this.instanceService.getDeviceCount(instance._id);
  instance.elementCount = await this.instanceService.getElementCount(instance._id);
  await instance.save();
  if (index+1 === instances.length) fetchingDone.next(instances);
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM