Parse.com云作业:缺少条目且包含null的指针数组

时间:2014-09-01 16:25:33

标签: javascript arrays parse-platform fetch cloud-code

我有一个云函数,它循环遍历一个名为drives的类。此类中的每个元素都有一个数组,其中包含指向名为driveResults的类的指针。

我做了一个云工作,就像这样取出驱动器:

首先,我创建了一个名为fetchPaginated的函数,因此我可以通过1次调用获取超过1000个项目。

Parse.Collection.prototype.fetchPaginated = function(callbacks){
  var promise = new Parse.Promise();
  var result = [];
  var thisCol = this;

  var processCallback = function(res) {
    result = result.concat(res);
    if (res.length === thisCol.query._limit) {
      process(res[res.length-1].id);
    }else{
      thisCol.reset(result);
      delete thisCol.query._where.objectId;
      if(callbacks && callbacks.success)
        callbacks.success();
      promise.resolve(true);
    }
  }
  var process = function(skip) {
    if (skip) {
      thisCol.query.greaterThan("objectId", skip);
    }
    thisCol.query.ascending("objectId");
    thisCol.query.find().then(function querySuccess(res) {
      processCallback(res);
    }, function queryFailed(reason) {
      if(callbacks && callbacks.error)
        callbacks.error(thisCol, reason);
      promise.reject("query unsuccessful, length of result " + result.length + ", error:" + reason.code + " " + reason.message);
    });
  }
  process(false);

  return promise;
};

其次,我为驱动器创建模型,查询和集合。

// ---------------------------------------------------------------------------------- Drives
// --------------------------------------------------- MODEL
var DrivesModel = Parse.Object.extend({
  className: "Drives",
});
exports.DrivesModel = DrivesModel;
// --------------------------------------------------- QUERY
var DrivesQuery = new Parse.Query(DrivesModel);
exports.DrivesQuery = DrivesQuery;
DrivesQuery.limit(1000);
DrivesQuery.addAscending('date');
DrivesQuery.include('driveResults');
DrivesQuery.find({
  success: function(results) {
    // results is an array of Parse.Object.
  },

  error: function(error) {
    // error is an instance of Parse.Error.
  }
});
// --------------------------------------------------- COLLECTION
var DrivesCollection = Parse.Collection.extend({
  model: DrivesModel,
  query: DrivesQuery,
});

exports.DrivesCollection = DrivesCollection;
var drivesCollection = new DrivesCollection();
exports.drivesCollection = drivesCollection;

然后,在我承诺的云功能中,我有这个:

    // some initializing code here... 
    return iEVCollections.drivesCollection.fetchPaginated();
}).then(function(){
    // at this point, all drives have been fetched.
    // some code here.

如您所见,DrivesQuery包含driveResults。

问题是:数组driveResuls包含null。如果我将它打印到控制台,这是数组的一部分:

[null,{"A Valid":"Object"},null,null,{"A Valid":"Object"},null,{"A Valid":"Object"},{"A Valid":"Object"},null,,null,null,{"A Valid":"Object"}]

在数据浏览器中,我可以看到这个数组内部绝对没有null,当我在骨干应用程序上执行相同操作时,它只包含有效的entires。那么为什么我的云功能缺少这些元素呢?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

(注意:在写这个问题时我找到了解决方案,然后我只是不想删除它。答案就像愚蠢一样简单)

DrivesQuery.limit(1000);将驱动器的查询限制设置为1000.每个驱动器至少有15个driveResults。最大查询限制1000也适用于行DrivesQuery.include('driveResults');隐含的嵌套查询。

感谢分页抓取功能,我可以将限制更改为50,然后所有结果都在那里,我得不到null