解析云代码助手功能不起作用 [英] Parse Cloud Code Helper Function Not Working

查看:68
本文介绍了解析云代码助手功能不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我编写的解析javascript云代码.我想在子类"Example"中找到所有具有相似对象的对象,然后将其重置为4.我已经在数据浏览器中建立了Example类和Like列.但是查询没有用,我不知道为什么.

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 Example class and Like column in the data browser. 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();
  });

我在iOS设备上使用这段代码来触发云功能:

I use this period of code on iOS device to trigger the cloud function:

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

推荐答案

几个可能的问题.

  1. 您正在调用异步方法,但没有给它们时间来完成.那就是解析承诺"出现的地方.您必须确保使用then函数.参见 http://blog.parse.com /2013/01/29/whats-so-great-about-javascript-promises/
  2. 您正确地将'Like'设置为4,但是您没有通过调用save
  3. 保存行
  4. 您的查询中可能没有任何行,它们可以用来检查是通过成功回调将找到的行数传递回去,
  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

请在下面尝试此操作,请注意,如果您从物镜c中NSLog(@"result %@",result),则.success应该返回结果.另外,由于response.error(error)

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

这篇关于解析云代码助手功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆