使用Promises的Parse Cloud代码的多个查询

时间:2015-05-18 17:31:00

标签: backbone.js parse-platform cloud-code

我有两个问题:

  1. 以下示例是在单个Cloud Code函数中执行多个Parse查询的正确方法吗?
  2. 以下示例是否会提供我使用一个 HTTP请求查询的所有数据(当我调用var gameScore = PFObject(className: "SiteLog-\(jobName.text)") gameScore["TypeOfWorks"] = "\(typeOfWorks.text)" gameScore["DateAndTime"] = "\(formattedDate)" gameScore.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in if (success) { // The object has been saved. println("sucessfully sent to parse!") } else { // There was a problem, check error.description println("Error Sending to Parse = \(error)") } 时),然后计为两个解析我帐户的请求,因为它有两个Parse查询?
  3. 以下是代码:

    logbookEntries

    提前致谢。

2 个答案:

答案 0 :(得分:4)

  1. 这是一种方式,虽然不太正确。
  2. 您的代码应该是:

    Parse.Cloud.define("logbookEntries", function(request, response) {
    
    //::: Query 1 :::
    var firstQuery = new Parse.Query("Logbook");
    var returnData = []; 
    
    firstQuery.find().then(function(firstResults) {
       returnData[0] = firstResults; 
    
       var secondQuery = new Parse.Query("Logbook"); 
       return secondQuery.find();
    }).then(function(result) {
       returnData[1] = result; 
    
       response.success(returnData);
    
    }, function(error) {
       response.error(error);
    });
    
    });
    

    或者,更好的结构方式是:

    Parse.Cloud.define("logbookEntries", function(request, response) {
    
    var firstQuery = new Parse.Query("Logbook");
    var secondQuery = new Parse.Query("Logbook");
    
    var promises = [];
    promises.push(firstQuery.find());
    promises.push(secondQuery.find());
    
    Parse.Promise.when(promises).then(function(result1, result2) {
       var returnData = [];
       returnData[1] = result1; 
       returnData[2] = result2; 
    
       response.success(returnData);
    
    }, function(error) {
       response.error(error);
    });
    
    }
    

答案 1 :(得分:0)

只是为了更新Wain的结构化代码: Promise.when在提供数组时返回数组,因此正确的代码将是

if(!value) {
   array.push(0)
} else {
   array.push(value)
}

[12,20,30,0,32,0,0,5]

并且因为不需要重新打包数组,所以它只是

Parse.Promise.when(promises).then(function([result1, result2]) {

See here了解更多信息。

相关问题