parse.com - Parse.User query not working in Cloud Code -


i working on project using parse need information calculated each user , updated when update account. created cloud code trigger need whenever user account updated, , working well. however, have 2 thousand accounts created need update well. after hours of trying cloud job work, decided try simplify it. wrote following job count user accounts. reiterate; i'm not trying count users, there more efficient ways that, trying verify can query , loop on existing user accounts. (the option usemasterkey in there because need later.)

parse.cloud.job("getuserstatistics", function(request, status) {     // set modify user data     parse.cloud.usemasterkey();     // query users     var query = new parse.query(parse.user);     var counter = 0;     query.each(function(user) {         counter = counter+1;     }).then(function() {         // set job's success status         status.success("counted user accounts.");     }, function(error) {         // set job's error status         status.error("failed count user accounts.");     });     console.log('found '+counter+' users.'); }); 

when run code, get:

i2015-07-09t17:29:10.880z]found 0 users. i2015-07-09t17:29:12.863z]v99: ran job getuserstatistics with:   input: "{}"   result: counted user accounts. 

even more baffling me, if add:

query.limit(10); 

...the query fails! (i expect count 10 users.)

that said, if there simpler way trigger update on users in parse application, i'd love hear it!

  1. the reference says that:

the query may not have sort order, and may not use limit or skip.

https://parse.com/docs/js/api/symbols/parse.query.html#each

so forget "query.limit(10)", that's not relevant here.

  1. anyways, example background job, seems might have forgotten put return in "each" function. plus, called console.log('found '+counter+' users.'); out side of asynchronous task, makes sense why 0 results. maybe try:

    query.each(function(user) {     counter = counter+1;     // you'll want save changes each user,     // therefore, need     return user.save(); }).then(function() {     // set job's success status     status.success("counted user accounts.");     // console.log inside asynchronous scope     console.log('found '+counter+' users.'); }, function(error) {     // set job's error status     status.error("failed count user accounts."); }); 

you can check again parse's example of writing cloud job.

https://parse.com/docs/js/guide#cloud-code-advanced-writing-a-background-job


Comments