javascript - Node.js bluebird.map 意外结果

标签 javascript node.js asynchronous bluebird

我一直在使用node.js,我需要使用jsons检查数组并更新一些项目,最后返回更新后的数组。 我正在做的事情如下:(主应用程序使用express和sequelize)

exports.getDetails = function (req, res) {
  var accountsArray = [];

  bluebird.map(req.accounts, function(account, callback){
     if (account.type == 'A'){
         if (account.param == req.body.paramSearch){
            account.updateAttributes({
                   param: req.body.paramSearch
            }).then(function(updatedAccount){
                console.log('A type entry');
                accountsArray.push(updatedAccount);
            });
         }
         else{
            console.log('A type entry');
            accountsArray.push(account);
         }
     }
     if (account.type == 'B'){
         if (account.param == req.body.paramSearch){
            account.updateAttributes({
                   param: req.body.paramSearch
            }).then(function(updatedAccount){
                console.log('B type entry');
                accountsArray.push(updatedAccount);
            });
         }
         else{
            console.log('B type entry');
            accountsArray.push(account);
         }
     }
  }).then(function(){
     console.log('Bluebird.map done');
     res.status(200).json({
        data: {
           accounts: accountsArray
        }
     });
  });

我的目的是首先将所有帐户(更新或未更新的受害者)添加到accountsArray中,然后才使用accountsArray返回json。

相反,我得到的是一个空的帐户Ary。我在代码中使用了 console.logs 来跟踪事情的处理方式。我总是得到类似的东西

Bluebird.map done
A/B type entry
...
A/B type entry

预期是

A/B type entry
...
A/B type entry
Bluebird.map done

我是 Node.js 新手,这种异步处理仍然让我感到困惑。 无论如何,有没有办法确保我已经完成了 map 部分内的子功能,然后才继续进行响应?如果可能的话,使用 Bluebird。

非常感谢任何帮助。

最佳答案

虽然 map 有效,但您在用例中想要的是 bluebird.each ,它旨在用于具有副作用的函数中。另一个问题是,您没有返回内部 promise 。请参阅下面的修复。

 bluebird.each(req.accounts, function(account){
     if (account.type == 'A'){
         if (account.param == req.body.paramSearch){
            return account.updateAttributes({
                   param: req.body.paramSearch
            }).then(function(updatedAccount){
                console.log('A type entry');
                accountsArray.push(updatedAccount);
            });
         }
         else{
            console.log('A type entry');
            accountsArray.push(account);
         }
     }
     if (account.type == 'B'){
         if (account.param == req.body.paramSearch){
            return account.updateAttributes({
                   param: req.body.paramSearch
            }).then(function(updatedAccount){
                console.log('B type entry');
                accountsArray.push(updatedAccount);
            });
         }
         else{
            console.log('B type entry');
            accountsArray.push(account);
         }
     }
  }).then(function(){
     console.log('Bluebird.map done');
     res.status(200).json({
        data: {
           accounts: accountsArray
        }
     });
  });

关于javascript - Node.js bluebird.map 意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32000079/

相关文章:

java - findViewByID 在 FirestoreRecyclerAdapter 中返回 null

javascript - 使用 fopen() 将文件内容从 PHP 回显到 javascript,file_exists() 不起作用

node.js - 通过父键查询实体

javascript - 从 HTML 调用 Node 请求

javascript - 如何 fork 另一个模块的进程

javascript - 在从查找返回之前等待 mongo 写入

javascript - TypeScript 可以为 Closure Compiler 输出注释吗?

javascript - Bootstrap 多个下拉菜单问题

javascript - Angular + Selenium : Cannot get value from input field

c# - 如何在 C# 中对异步文件 IO 进行排队?