gpt4 book ai didi

javascript - meteor 集合.update 没有更新文档

转载 作者:行者123 更新时间:2023-12-03 10:24:36 24 4
gpt4 key购买 nike

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

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

关于javascript - meteor 集合.update 没有更新文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29460880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com