Skip to content Skip to sidebar Skip to footer

How Do You Push 10,000 Results Into An Array Before Moving On In Parse.com

I have a Parse.com query and a function where I want to send the data from the Parse.com query. I need to sum all of the results from the table and it returns more than 1,000 Parse

Solution 1:

Instead of recursion, you can use an array.reduce(...) pattern to fetch your results in batches.

As there's no naturally occurring array to reduce, you need to synthesise one, with one element per batch. The array elements could be anything you like but it's convenient for them to be the batch-size, with any remainder as the final element. The values in the array will be the limit passed to query.limit().

functionfindInBatches(N, batchSize) {
    //First apportion N into an array of limits, //each of bachSize, and finally any remainder.var limits = [];
    for(var nn = N; nn > 0; nn -= batchSize) {
        limits.push(Math.min(nn, batchSize));
    }

    // Now, the limits array can be reduced to build a promise chain,// which progressively accumulates results in an array as the query.find() promises settle.var accumulator = [];
    return limits.reduce(function(promise, limit, i) {
        var query = newParse.Query(Parse.User);
        query.limit(limit);
        query.skip(i * batchSize);
        return promise.then(function() {
            return query.find().then(function (results) {
                if (results && results.length) {
                    accumulator = accumulator.concat(results);
                } else {
                    returnParse.Promise.error('Premature end of results');
                }
            });
        });
    }, Parse.Promise.as()) // Parse.Promise.as() gets the chain started.
    .then(function() {
        return accumulator;
    }, function (e) { // error handlerconsole.log(e);
        if (accumulator.length) {
            return accumulator; //return any results fetched before the error occurred.
        } else {
            returnParse.Promise.error('No results found');//explain what went wrong.
        }
    });
}

Call as follows :

findInBatches(10000, 1000).then(function(results) {
    //use `results` here 
}, function(e) {
    //an error occurred. 'No results found' or something unpredicted
});

Solution 2:

So basically you need to create an array outside of the function that loops over the Parse.com and then push the results into it in order to fetch all the records. Then you need to execute the next function which will sum or do the final calculation. Notice that I don't know anything abou how parse.com works, this is simple javascript and it should work if you can adequate this solution to your specific problem:

var new_array = [], total_records = 35000, query = newParse.Query("Table");

while (total_records > 10000){
    query.find().then(function(results){
        new_array.push(results);
    });
}

last_function(new_array);

Post a Comment for "How Do You Push 10,000 Results Into An Array Before Moving On In Parse.com"