简体   繁体   English

使用Parse Code Cloud Javascript函数更改Parse列的值

[英]Changing values of Parse column using Parse Code Cloud Javascript function

So its my first time writing Javascript so please bear with me. 所以这是我第一次编写Javascript,所以请多多包涵。 I wrote this function in order to query a class in my Parse.com application, and then after querying I want to set one of the columns (of type Boolean) to true. 我编写此函数是为了查询我的Parse.com应用程序中的类,然后在查询后我想将列(布尔类型)之一设置为true。

I set up a test class with only 7 values in order to test. 我只设置了7个值的测试类进行测试。

The problem: only 3 out of 7 are being changed. 问题是:7个中只有3个被更改。 Do I have to wait after each save? 每次保存后是否需要等待? I know that waiting/sleeping in Javascript is "wrong" but I can't seem to find a solution. 我知道Javascript中的等待/睡眠是“错误的”,但是我似乎找不到解决方案。

Thanks in advance! 提前致谢!

Additionally, when using iOS/Parse, I would like to check if the boolean value is undefined in Objective-C, I already tried to compare it to nil/NULL, an exception was thrown 另外,当使用iOS / Parse时,我想检查布尔值在Objective-C中是否未定义,我已经尝试将其与nil / NULL进行比较,并引发了异常

Parse.Cloud.define("setYears", function(request, response) {
var object = new Parse.Query("testClass");

object.find({
success: function(results)
{
for (var i = 0; i < results.length; i++) {
    results[i].set("testBool",true);// = true;
    results[i].save(null,

         {
            success:function ()
            {
                response.success("Updated bool!");
            },
            error:function (error)
            {
                response.error("Failed to save bool. Error=" + error.message);
            }

        });

};

response.success();

}
})
});

It turned out to be not that difficult to solve as stated above. 事实证明,如上所述并不难解决。 Just had to use saveAll instead of saving each object by itself. 只需使用saveAll而不是单独保存每个对象。 Here is the correct solution if anybody needs it: 如果有人需要,这是正确的解决方案:

Parse.Cloud.define("setYears", function(request, response) {
var object = new Parse.Query("testClass");

object.find({
success: function(results)
{
for (var i = 0; i < results.length; i++) {
    results[i].set("testBool",true);// = true;  
}

Parse.Object.saveAll(results,{
success: function(list) {
  // All the objects were saved.
  response.success("ok " );  //saveAll is now finished and we can properly exit with confidence :-)
},
error: function(error) {
  // An error occurred while saving one of the objects.
  response.error("failure on saving list ");
},
});


response.success();

}


  })

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM