Get Data from parse.com Promises -


i trying use parse.com promises retrieve job data user relation data associated job. have function returns promises not job data. how job & employee information returned promises?

logically want to:
1) query parse array of jobs
2) each job, query parse again employee relation information
3) create local object contains job & employee details
4) add each job object local array
5) load table array of objects once information has been retrieved parse

i can steps 1-4 can't figure out how wait until information has been retrieved parse refresh local table.

function getjobpromises (){        var promises = [];        var job = parse.object.extend("job");       var query = new parse.query(job);       query.equalto("company", company);       query.notequalto("isdeleted", true);       query.limit(1000); // raise limit max amount       query.find().then(function(results) {         // create trivial resolved promise base case.         var promise = parse.promise.as();          _.each(results, function(result) {           // each item, extend promise function add job array             promise = promise.then(function() {               // return promise resolved when job details have been added array                   var object = result;                  promises.push(getemployeename(object));                   alljobdataarray = promises;               });          });          return parse.promise.when(promises);        }).then(function() {         // every job has been retrieved         console.log("all items have been returned. refresh table...");         console.log(alljobdataarray);         }); } 

the function relational query users associated job

function getemployeename(jobobject) {      var employeenamearray = [];        //query array of employees passed in job       var rquery = jobobject.relation("employee");       return rquery.query().find({         success: function(employees){              //get employees full name each job             (var = 0; < employees.length; i++) {               var objemployee = employees[i];               var fullname = objemployee.get("fullname");               employeenamearray.push(fullname);               console.log(employeenamearray);               }         },         error: function(error){           response.error(error);         }        });       } 

update working @eduardo

i have public array hold job objects.

var jobobjectsarray = [];

in getemployeename function creating job objects , adding them array

function getjobpromises (){         var promises = [];         var job = parse.object.extend("job");         var query = new parse.query(job);         query.equalto("company", company);         query.notequalto("isdeleted", true);         query.limit(1000); // raise limit max amount         query.find().then(function(results) {           _.each(results, function(result) {             promises.push(getemployeename(result));           });            return parse.promise.when(promises);          }).then(function(alljobdataarray) {           // alljobdataarray should array of array           console.log(jobobjectsarray);           refreshtable();          });       }        function getemployeename(jobobject) {         var employeenamearray = [];          //query array of employees passed in job         var rquery = jobobject.relation("employee");          return new promise(           function(resolve, reject) {             rquery.query().find({               success: function(employees){                 //get employees full name each job                 (var = 0; < employees.length; i++) {                   var objemployee = employees[i];                   var fullname = objemployee.get("fullname");                   employeenamearray.push(fullname);                    var objalljobs = new object();                    objalljobs["jobid"] = jobobject.id;                   objalljobs["location"] = jobobject.get("location");                   objalljobs["starttime"] = jobobject.get("starttime");                   objalljobs["employee"] = employeenamearray;                    jobobjectsarray.push(objalljobs);                  }                 console.log(employeenamearray);                 resolve(employeenamearray);               },               error: function(error){                 reject(error);               }             });           }         ); 

there few incorrect uses of promise concept. go through them, first here final code:

function getjobpromises (){   var promises = [];   var job = parse.object.extend("job");   var query = new parse.query(job);    query.equalto("company", company);   query.notequalto("isdeleted", true);   query.limit(1000); // raise limit max amount   query.find().then(function(results) {      _.each(results, function(result) {       promises.push(getemployeename(result));     });      return parse.promise.when(promises);    }).then(function(alljobdataarray) {     // alljobdataarray should array of array     console.dir(alljobdataarray);     console.log(alljobdataarray[0]);   }); }  function getemployeename(jobobject) {   var employeenamearray = [];    //query array of employees passed in job   var rquery = jobobject.relation("employee");    return rquery.query().find({     success: function(employees){        //get employees full name each job       (var = 0; < employees.length; i++) {         var objemployee = employees[i];         var fullname = objemployee.get("fullname");         employeenamearray.push(fullname);       }       console.log(employeenamearray);       return employeenamearray;     },     error: function(error){       response.error(error);     }   }); } 

"parse.promise.as()" should used if have value want return promise. like:

parse.promise.as("my value").then(function(foo) {   console.log(foo) // "my value" }); 

so if "getemployeename" function returning promise, means "rquery.query().find" returns promise, don't have create new promise or use "parse.promise.as()", promise , can push promises array.

another problem not return in "getemployeename" method callback. take version, i'm returning "employeenamearray".

my version work if "rquery.query().find" method returns promise. if not case, can create new promise using callbacks this:

function getemployeename(jobobject) {   var employeenamearray = [];    //query array of employees passed in job   var rquery = jobobject.relation("employee");    return new promise(     function(resolve, reject) {       rquery.query().find({         success: function(employees){           //get employees full name each job           (var = 0; < employees.length; i++) {             var objemployee = employees[i];             var fullname = objemployee.get("fullname");             employeenamearray.push(fullname);           }           console.log(employeenamearray);           resolve(employeenamearray);         },         error: function(error){           reject(error);         }       });     }   ); } 

please notice "new promise()" depends on browser support of promise, don't know if parse has equivalent. anyways can use polyfill implements necessary code if browser has no support.

more standard promises: https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/promise

polyfill: https://github.com/jakearchibald/es6-promise/

hope helps.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -