根据Meteor集合中的数量和价格更新合计

Updating total from quantity and price in Meteor collection

本文关键字:更新 集合 Meteor 根据      更新时间:2023-09-26

我有三个字段quantitypricetotal

我只更新quantityprice,所以total应该自动计算。

如何确保total始终正确更新?我想我应该用一个收集钩。

如果您使用的是自动表单和简单模式,只需使用自动值

'price': { type: Number },
'quantity': { type: Number },
'total': {
  type: Number,
  autoValue: function () {
    const price = this.field('price');
    const quantity = this.field('quantity');
    if (price.isSet && quantity.isSet) {
      if (this.isInsert) {
        return quantity.value * price.value;
      } else {
        return { $set: quantity.value * price.value };
      }
    }
  }  
}