【问题标题】:Meteor - Mongo won't update, object in arrayMeteor - Mongo 不会更新,数组中的对象
【发布时间】:2016-11-23 17:04:35
【问题描述】:

我在 Meteor 中使用 MongoDB 时遇到了一些问题。基本上,我想更新嵌套在数组中的对象的属性。整个对象的结构如下:

product {
 _id: 
 some other properties
 template: {
    some other properties
    additionalFields: [
      { _id: 
        content: **update this**
      }
    ]
 }
}

我写了下面的 Meteor 方法:

 'products.update.field.content': function(product, field, update) {
    return Products.update(
        {_id: product._id, 'template.additionalFields._id': field._id},
        {$set: {'template.additionalFields.$.content': update }});
}

并调用我使用的方法

onInputChange(event) {
    const { product, field } = this.props;
    Meteor.call('products.update.field.content', product, field, event.target.value);
}

如果我在我的方法调用中放置回调(error, update) => console.log(error, update),我会收到(未定义,1)。然而,当我 console.log 产品对象内容没有改变。

有人可以帮忙吗? 谢谢

【问题讨论】:

  • 应该可以,也许对象没有足够的时间来更新或者将它映射到你的道具的查询没有反应。
  • 是的,@MasterAM 使用非反应性道具的建议做到了。发现错误,是相关的。非常感谢,我花了很长时间才找到这个。

标签: javascript mongodb meteor


【解决方案1】:

你可以试试这个:

Products.update({
  _id: product_id,
  'template.additionalFields': {
    $elemMatch: {
      _id: field._id
    }
  }
}, {
  $set: {
    'template.additionalFields.$.content': update
  }
});

或者你可以去链接了解更多详情 https://docs.mongodb.com/v3.2/reference/operator/query/elemMatch/

【讨论】:

    猜你喜欢
    • 2018-10-15
    • 2015-05-05
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多