I'm working with nodejs and sequelize framework but I'm having issues trying to retrieve some data
getAllMedicamentsWithTotal: function () {
return medicamentService.getAll().then(function success(medicaments) {
for(var i = 0 ; i < medicaments.length; i++){
getTotalMedicament(medicaments[i]).then(function (total) {
medicaments[i].dataValues.total = total;
})
}
}, function fail() {
})
}
I have this function who gets all the medicament with its total in the stock. But the outer promise end before the callback its executed. I know there is an error over looping promises but is there a better way to do this than fixing this piece of code?
To give a little more context I have a table inventory who has a foreign key to the table medicaments, and in the inventory table there is the quantity of the medicaments. I want to get something like [{name1, sum(stockQuantity1)}, {name2, sum(stockQuantity2)}, ...] but I cant manage to do so.
I would appreciate any help