【问题标题】:Parse.com cloud job: Array of pointers missing entries and containing nullParse.com 云作业:指针数组缺少条目并包含空值
【发布时间】:2014-09-01 16:25:33
【问题描述】:

我有一个云函数,它循环一个名为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;

然后,在我的云函数里面有一个promise,我有这个:

    // 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,当我在骨干应用程序上做同样的事情时,它只包含有效的整体。那么为什么我的云功能缺少这些元素呢?

感谢您的帮助。

【问题讨论】:

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


    【解决方案1】:

    (注:在写问题的时候找到了解决方法,然后我就是不想删了。答案很简单,很愚蠢)

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

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

    【讨论】:

    • 是的,这让我发疯了好几天......直到我决定在这里发布它,而答案只是通过写作来了。我被堆栈溢出启发了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 2014-06-17
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多