Skip to content

Instantly share code, notes, and snippets.

@DinoChiesa
Created February 11, 2015 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DinoChiesa/8f5958f735104a647842 to your computer and use it in GitHub Desktop.
Save DinoChiesa/8f5958f735104a647842 to your computer and use it in GitHub Desktop.
/*
* ugCollectionForEach
*
* Iterates through all items IN A SINGLE PAGE of a collection within
* Apigee Edge BaaS. Uses the Usergrid client object from the usergrid
* module. Notice - this logic does not call {has,get}NextPage().
*
* @param ugClient - the authenticated client object
* @param options - the options for a collection. Pass type and qs.
* @param f - function called with each UG entity. Accepts a single argument.
* @param doneCb - called in case of error or success.
*
*********************************************/
function ugCollectionForEach (ugClient, options, f, doneCb) {
var results = {count: 0, failCount: 0};
if ( ! options.qs) {
doneCb(new Error('missing qs property in the options argument'), null);
}
if ( ! options.type) {
doneCb(new Error('missing type property in the options argument'), null);
}
ugClient.createCollection(options, function (e, collection) {
var e2;
if (e) {
e2 = new Error('could not make or get collection');
e2.wrappedError = e;
doneCb(e2, null);
}
else {
// we got the collection, now examine each of the Entities:
while(collection.hasNextEntity()) {
f(collection.getNextEntity(), results);
results.count++;
}
doneCb(null, results);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment