流星收集更新不是在更新文档

meteor collection.update is not updating document

本文关键字:更新 文档 流星      更新时间:2023-09-26

我正试图在客户端方法中触发更新(考虑稍后进入服务器),如下所示:

 Meteor.methods({
    // Calling this and passing in a currentSelected value = "avatar" on click
    'updateSelectedDocument' : function(currentSelected) {
      var current = LayoutVariations.findOne({elementID: currentSelected});
      var index = current.currentIndex;
      myCollection.update({_id :current._id}, {currentIndex: 2});
    }
 });

.update应该找到文档并更新该文档的currentIndex属性,该属性是一个整数。

我在控制台中通过传递_id(例如"GRvujvgBEmem3Dp3d")运行了myCollection.update({_id :current._id}, {currentIndex: 2});,它就工作了。当我在方法中调用它时,它不会更新,也不会抛出任何错误。

想知道问题出在哪里。

在更新中使用$set运算符将字段currentIndex的值替换为指定的:

Meteor.methods({
     // Calling this and passing in a currentSelected value = "avatar" on click
     'updateSelectedDocument' : function(currentSelected) {
      var current = LayoutVariations.findOne({elementID: currentSelected});
      var index = current.currentIndex;
      myCollection.update({_id :current._id}, {$set: { currentIndex: 2 } }, function(error, affectedDocs) {
          if (error) {
              throw new Meteor.Error(500, error.message);
          } else {
              return "Update Successful";
          }
      });
    }
 });