i’m new parse , cloud code. i’m trying initialize new user’s data via cloud code function. function queries “initial items objects” (a total of 223 items), loops through each 1 creating new “item” user. inside loop new item’s fields set , ends calling save(). function seems working, however, instead of saving 223 new items, 9 created. placed log statement after save see if in fact loop iterating through 223 items … , is. below cloud code , log.
any thoughts why 9 items being saved? 9 items saved fine.
parse.cloud.define("initializenewuser", function(request, response) { var initialitemsobject = parse.object.extend("initialparseitems"); var itemsquery = new parse.query(initialitemsobject); itemsquery.limit(500); itemsquery.find().then(function(results) { console.log('found ' + results.length +' items.'); var user = request.user; var acl = new parse.acl(user); (var = 0; < results.length; i++) { var defaultitem = results[i]; var item = new parse.object("items"); item.set('itemid', defaultitem.get('itemid')); item.set('author', user); item.set('groupid', defaultitem.get('groupid')); item.set('itemname', defaultitem.get('itemname')); item.set('itemnote', defaultitem.get('itemnote')); item.set('itemchecked', defaultitem.get('itemchecked')); item.set('itemisfavorite', defaultitem.get('itemisfavorite')); item.set('itemselected', defaultitem.get('itemselected')); item.set('itemstruckout', defaultitem.get('itemstruckout')); item.set('manualsortorder', defaultitem.get('manualsortorder')); item.set('productid', defaultitem.get('productid')); item.set('itemtimestamp', defaultitem.get('itemtimestamp')); item.setacl(acl); item.save(); //console.log(defaultitem.get('itemname') + ' saved.'); } // success has been moved inside callback query.find() console.log('successfully initialize ' + results.length + ' items.'); response.success(results.length); }, function(error) { // make sure catch errors, otherwise may see "success/error not called" error in cloud code. console.log('failed initialize items. error code: ' + error.code + ' ' + error.message); response.error("failed initialize items. error code: " + error.code + ": " + error.message); }); }); i2015-07-17t15:16:38.661z]v22 ran cloud function initializenewuser user va0ttgwok7 with: input: {} result: 223 i2015-07-17t15:16:38.930z]found 223 items. i2015-07-17t15:16:39.141z]successfully initialize 223 items.
ok have few comments.
1.) on line 7 set user request.user. i'm not sure how sending data, i've used request.params.[insert item name]
2.) next big thing using "then()" using promises. @ end of every "then", should returning promise. means wouldn't "item.save()" "return item.save()". saving promise need return those. here should general promise pattern.
somepromise.then( function(a) {return promisea} ).then( function(b) {return promiseb} ).then( function(c) {response.success}, function(error){response.error}) 3.) when saving lots of items, either have use "saveall()" or make array of promises, save them @ once. rule of thumb use promise array every time saving lots of things in array. here portion of cloud code developer guide show correct format https://parse.com/docs/js/guide#promises-promises-in-parallel
Comments
Post a Comment