Parse Cloud Code Helper Function Not Working

后端 未结 1 1657
甜味超标
甜味超标 2021-01-26 09:30

Here is the parse javascript cloud code I wrote. I want to find all objects in subclass \"Example\" having one like and then reset them as four. I have already established the E

1条回答
  •  深忆病人
    2021-01-26 10:19

    A couple of possible issues..

    1. You are calling asynchronous methods and not giving them time to complete. That's where Parse Promises come in. You have to make sure to use the then function. See http://blog.parse.com/2013/01/29/whats-so-great-about-javascript-promises/
    2. You are correctly setting 'Like' to 4, but you aren't saving the rows by calling save
    3. You may not have any rows coming back from your query, they way to check that is to pass the number of rows found back through the success callback, which I am doing below

    Try this below, noticing that the .success should return a result if you NSLog(@"result %@",result) from your objective-c. Also, the error should be coming through now as well because of response.error(error)

    var Example = Parse.Object.extend("Example");
    
    function exampleFunction() {
      var query = new Parse.Query(Example);
      query.equalTo('Like',1);
      return query.find().then(function(examplesLikedOnce){
        var promises = [];
        for (var i = 0; i < examplesLikedOnce.length; i++) {
          var example = examplesLikedOnce[i];
          var promise = example.save({Like:4});
          promises.push(promise);
        }
        return Parse.Promise.when(promises).then(function(){
          return examplesLikedOnce.length;
        });
      }); 
    }
    
    Parse.Cloud.define("nice", function(request, response) {
        exampleFunction().then(function(numExamples){
          response.success("The number of Example objects with 1 like: "+numExamples);
        }, function(error){
          response.error(error);
        });
    });
    

    0 讨论(0)
提交回复
热议问题