简体   繁体   English

解析云代码助手功能不起作用

[英]Parse Cloud Code Helper Function Not Working

Here is the parse javascript cloud code I wrote. 这是我编写的解析javascript云代码。 I want to find all objects in subclass "Example" having one like and then reset them as four. 我想在子类“示例”中找到所有具有相似对象的对象,然后将其重置为四个。 I have already established the Example class and Like column in the data browser. 我已经在数据浏览器中建立了Example类和Like列。 But the query didn't work and I can't figure out why. 但是查询不起作用,我不知道为什么。

function exampleFunction() {
  var Example = Parse.Object.extend("Example");
  var newObject = new Example();
  newObject.save(); // until here, the function works, it continues creating new objects
  var query = new Parse.Query(Example);
  query.equalTo('Like',1);
  query.find({
    success:function(result){
      for (var i = 0; i < result.length; i++) {
          result[i].set('Like',4);
      }
    },
    error:function(error){

    }
  }) 
}

  Parse.Cloud.define("nice", function(request, response) {
      exampleFunction();
      response.success();
  });

I use this period of code on iOS device to trigger the cloud function: 我在iOS设备上使用这段代码来触发云功能:

[PFCloud callFunctionInBackground:@"nice"
                   withParameters:@{}
                            block:^(NSString *result, NSError *error) {
                                if (!error) {
                                }
                            }];

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. 那就是“解析承诺”出现的地方。您必须确保使用then函数。 See http://blog.parse.com/2013/01/29/whats-so-great-about-javascript-promises/ 参见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 您将'Like'正确设置为4,但没有通过调用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. 请尝试以下操作,请注意,如果您从Objective-c获得NSLog(@"result %@",result) ,则.success应该返回结果。 Also, the error should be coming through now as well because of response.error(error) 另外,由于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);
    });
});

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

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