0

I have a cloud function that loops over a class named drives. Each element in this class has an array with pointers to a class named driveResults.

I made a cloud job that fetches the drives like this:

First, I made a function called fetchPaginated so I can fetch more than 1000 items with 1 call.

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

Second, I create the model, query and collection for drives.

// ---------------------------------------------------------------------------------- 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;

Then, in my cloud function inside a promise, I have this:

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

As you can see, the DrivesQuery includes the driveResults.

The problem is: The array driveResuls contains null. This is a part of the array if I print it to the console:

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

In the data browser I can see this array has absolutely NO null inside, and when I do the same on a backbone app, it only contains valid entires. So why is my cloud function missing this elements?

Thanks for your help.

otmezger
  • 10,410
  • 21
  • 64
  • 90

1 Answers1

1

(Note: The solution came to me while writing the question, and then I just didn't want to delete it. The answer is as simple as it is stupid)

The line DrivesQuery.limit(1000); sets the limit of query for drives to 1000. Each drive has at least 15 driveResults. The max query limit of 1000 applies also for the nested queries implied by the line DrivesQuery.include('driveResults');.

Thanks to the pagination fetch function, I could just change the limit to 50, and then all results are there and I get no null

otmezger
  • 10,410
  • 21
  • 64
  • 90
  • 1
    yes, It was driving me crazy for days... until I decided to post it here, and the answer just came by writing. I got enlighted by stack overflow :) – otmezger Sep 01 '14 at 16:36