<http://stackoverflow.com/questions/31481546/q-promise-and-map-doesnt-change-after-iteration#>
 
  
I'm using Q Promises to retrieve data from my redis repository. The problem 
I'm having, is that through each iteration, the array object 
(localEncounter) I'm using to store data returned from the chained 
functions is never updated at each iteration. Previously, I tried to solve 
this with a foreach loop and spread but the results were the same.

How should I correct this so that localEncounter is updated at each 
iteration, and ultimately localEncounters contains correct data when 
returned? Thank you.


var localEncounters = [];
            var localEncounter = {};

            Promise.all(ids.map(function(id) {
                return localEncounter, getEncounter(id, client)
                    .then(function (encounter) {
                        encounterObject = encounter;

                        //set the fields for the return object
                        localEncounter['encounterid'] = encounterObject[f_id];
                        localEncounter['screeningid'] = 
encounterObject[f_screening_id];
                        localEncounter['assessmentid'] = 
encounterObject[f_clinical_assessment_id];
                        localEncounter['psychevalid'] = 
encounterObject[f_psych_eval_id];

                        //get screening
                        return getScreening(encounterObject[f_screening_id], 
client);
                    })
                    .then(function (screening) {
                        //set the fields for the return object
                        localEncounter['screeningbegintime'] = 
screening[f_begin_time];
                        //get assessment                        
                        return getAssessment(localEncounter['assessmentid'], 
client);
                    })
                    .then(function (assessment) {
                        //set the fields for the return object
                        localEncounter['assessmentbegintime'] = 
assessment[f_begin_time];
                        //get psycheval
                        //localEncounters.push(assessment);
                        return getPsychEval(localEncounter['psychevalid'], 
client);
                    })
                    .then(function (psychEval) {
                        //set the fields for the return object
                        localEncounter['psychevalbegintime'] = 
psychEval[f_begin_time];

                        localEncounters.push(localEncounter);

                    }
                    , function (reason) {
                        console.log(reason); // display reason why the call 
failed;
                        reject(reason, 'Something went wrong creating the 
encounter!');
                    })

            })).then(function(results) {
                // results is an array of names
                console.log('done ');

                resolve(localEncounters);
            })

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/aeabd7f6-1200-47d9-8b10-469dd4fe4bc6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to