2015-04-05 133 views
0

我试图触发客户端方法中的更新(思后进入服务器)如​​下:流星collection.update没有更新文件

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});,它可以工作。当我在方法中调用它时,它不会更新,也不会引发任何错误。

想知道可能是什么问题。

回答

0

使用$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"; 
      } 
     }); 
    } 
}); 
+0

感谢您的回答。我试过'$ set'运算符。但是我看到了同样的行为。运行'myCollection.update({_ id:current._id},{$ set:{currentIndex:2}});'(用实际的_id)返回1并更新文档。但是从函数内部更新它似乎并没有更新数据库中的文档。 – Poyi 2015-04-05 22:42:17

+0

你在哪里调用客户端的方法'Meteor.call(“updateSelectedDocument”,function(error,result){...});'? – chridam 2015-04-05 22:47:04

+0

只是一个模板点击事件'' Template.frame.events({ '点击按钮':函数(){ Meteor.call( 'updateSelectedDocument',currentSelected);} });内'' 我只是将该方法移入服务器(“服务器”文件夹下的文件)。它似乎正确更新。看起来CRUD方法只适用于服务器端呢? – Poyi 2015-04-05 23:01:27