如何使用 javascript 更新 parse.com 中的当前对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13251955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can i update current object in parse.com with javascript?
提问by AzabAF
I want to update object i already have in parse.com with javascript; what i did is i retirevied the object first with query but i dont know how to update it.
我想用 javascript 更新我在 parse.com 中已有的对象;我所做的是我首先使用查询退休了对象,但我不知道如何更新它。
here is the code i use, whats wrong on it?
这是我使用的代码,有什么问题吗?
var GameScore = Parse.Object.extend("Driver");
var query = new Parse.Query(GameScore);
query.equalTo("DriverID", "9");
query.find({
success: function(results) {
alert("Successfully retrieved " + results.length + "DName");
results.set("DName", "aaaa");
results.save();
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
回答by Hairgami_Master
The difference between the question and your answer may not be obvious at first- So for everyone who has happened here- Use query.firstinstead of query.find.
问题和你的答案之间的区别一开始可能并不明显 - 所以对于在这里发生过的每个人 - 使用query.first而不是query.find。
query.find() //don't use this if you are going to try and update an object
returns an array of objects, an array which has no method "set" or "save".
返回一个对象数组,一个没有“set”或“save”方法的数组。
query.first() //use this instead
returns a single backbone style object which has those methods available.
返回一个具有可用方法的主干样式对象。
回答by AzabAF
I found the solution, incase someone needs it later
我找到了解决方案,以防以后有人需要它
here it is:
这里是:
var GameScore = Parse.Object.extend("Driver");
var query = new Parse.Query(GameScore);
query.equalTo("DriverID", "9");
query.first({
success: function(object) {
object.set("DName", "aaaa");
object.save();
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
回答by Nam Le
If someone got msg "{"code":101,"error":"object not found for update"}", check the class permission and ACL of Object to enrure it's allowed to read and write
如果有人收到 msg "{"code":101,"error":"object not found for update"}",请检查 Object 的类权限和 ACL 以确保它可以读写
回答by Daman
Do something like this:
做这样的事情:
var GameScore = Parse.Object.extend("Driver");
var query = new Parse.Query(GameScore);
query.equalTo("DriverID", "9");
query.find({
success: function(results) {
alert("Successfully retrieved " + results.length + "DName");
// - use this-----------------
results.forEach((result) => {
result.set("DName", "aaaa");
});
Parse.Object.saveAll(results);
// --------------------------------
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
回答by Karen Mastoyan
You can do it like this:
你可以这样做:
var results= await query.find();
for (var i = 0; i < results.length; i++) {
results[i].set("DName", "aaaa");
results[i].save();
}