Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.js bluebird.map意外结果_Javascript_Node.js_Asynchronous_Bluebird - Fatal编程技术网

Javascript Node.js bluebird.map意外结果

Javascript Node.js bluebird.map意外结果,javascript,node.js,asynchronous,bluebird,Javascript,Node.js,Asynchronous,Bluebird,我一直在使用node.js,我需要用json检查一个数组,并更新一些项,最后返回更新后的数组。 我正在做的事情如下:(主应用程序使用express和sequelize) 我打算它首先将所有帐户(无论是否是更新的受害者)添加到accountsArray,然后才返回带有accountsArray的json 相反,我得到的是一个空账户。我在代码中使用了console.logs来跟踪处理的方式。我总是得到这样的东西 Bluebird.map done A/B type entry ... A/B typ

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

我打算它首先将所有帐户(无论是否是更新的受害者)添加到accountsArray,然后才返回带有accountsArray的json

相反,我得到的是一个空账户。我在代码中使用了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。每个
都用于具有副作用的函数中。另一个问题是,您没有
返回
内部承诺。请参阅下面的修复

 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
        }
     });
  });

虽然
map
可以工作,但在您的用例中,您需要的是
bluebird。每个
都用于具有副作用的函数中。另一个问题是,您没有
返回
内部承诺。请参阅下面的修复

 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
        }
     });
  });