循环以获取nodejs中的异步数据(Loop to get asynchronous data in nodejs sequelize)

编程入门 行业动态 更新时间:2024-10-28 06:24:18
循环以获取nodejs中的异步数据(Loop to get asynchronous data in nodejs sequelize)

我正在使用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.

更多推荐

本文发布于:2023-08-06 16:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454291.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   Loop   nodejs   sequelize   data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!