我正在使用nodejs和sequelize框架,但我在尝试检索一些数据时遇到问题
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() { }) }我有这个功能,他在药物中获得所有药物的总量。 但是外部承诺在回调执行之前就结束了。 我知道循环承诺有一个错误,但有没有比修复这段代码更好的方法呢?
为了给出更多的背景,我有一个表库存,其具有表药物的外键,并且在库存表中有药物的数量。 我希望得到像[{name1,sum(stockQuantity1)},{name2,sum(stockQuantity2)},...]这样的东西,但我无法做到这一点。
我将不胜感激任何帮助
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
最满意答案
你必须在这样的承诺中包装所有东西
getAllMedicamentsWithTotal: function () { return new Promise((resolve, reject) => { // call your service first medicamentService .getAll() // with your array of results we need to map out an array of promises // then feed that array into Promise.all() .then(medicaments => Promise.all(medicaments.map(med => { return getTotalMedicament(med); })) // the result of Promise.all() is an array of results // reduce the totals to get your accumulated total and resolve it to the caller .then(arrayWithTotals => { let total = arrayWithTotals.reduce((acc, obj) => acc += obj.dataValues.total, 0); resolve(total); }); }); // elsewhere... getAllMedicamentsWithTotal().then(total => { /** your total */ });顺便说一句,看起来你正在为很有可能通过join查询完成的事情做很多逻辑。 IIRC sequelize将此属性称为查询对象上的include属性。
You have to wrap everything in a promise like this
getAllMedicamentsWithTotal: function () { return new Promise((resolve, reject) => { // call your service first medicamentService .getAll() // with your array of results we need to map out an array of promises // then feed that array into Promise.all() .then(medicaments => Promise.all(medicaments.map(med => { return getTotalMedicament(med); })) // the result of Promise.all() is an array of results // reduce the totals to get your accumulated total and resolve it to the caller .then(arrayWithTotals => { let total = arrayWithTotals.reduce((acc, obj) => acc += obj.dataValues.total, 0); resolve(total); }); }); // elsewhere... getAllMedicamentsWithTotal().then(total => { /** your total */ });As an aside it looks like you are doing a lot of logic for something that can most likely be done with a join query. IIRC sequelize calls this the include property on your query object.
更多推荐
发布评论