In Express I have an array of projects, with each project containing an array of workSessions stored as Mongo ObjectIds.
I want to iterate over the array of projects, and for each project get its workSessions from MongoDB, then add the list of workSessions to an object which will end up containing all the workSessions from all the projects in the projects array.
projectService.GetWorkSessions(result.user._id)
.then((projects) => {
console.log(projects) // nothing gets logged.
}
GetWorkSessions: (userId) => {
return getProjects(userId) // this is ok.
.then((projects) => {
let workSessions = {};
let counter = 0;
return promise = new Promise((resolve, reject) => {
return projects.forEach((project) => {
return getWorkSessions(project)
.then((sessionsList) => {
counter ++
workSessions[project._id] = sessionsList;
if(counter == projects.length) {
console.log('done')
promise.resolve();
}
})
})
})
})
},
The workSession object gets populated, but how do I return it to the calling function once the forEach has completed?
promise = new Promise...promise.resolve()- no ... that's not how Promises work