javascript - 为什么 async.each Nodejs 不能正常工作?

标签 javascript mysql node.js

我正在尝试使用 async.each 函数来获取包含两个查询结果的数组。之后,我需要在网页中呈现此结果。

async.each 函数正确计算变量结果,但是,我无法在函数外部导出该变量并渲染它,我不明白为什么。

这里我附上了代码,我在那里测试了它。我意识到,当我调用“callback1”时,函数(错误)不起作用,并且我没有在控制台中获取变量列表(因此我稍后将无法渲染它)。如果有人能帮助我,我将不胜感激。非常感谢。

    var list = [];

    async.each(data, 
    function(elem, callback1){
        var classgene = '';
        var custom_gene = {};
        custom_gene = {Name_Gene: elem['Name_Gene']};

        if (elem['Type_Gene'] == "reference") {
            async.waterfall([
            function(callback2){
                var id = elem['Id_Genes'];
                geneModel.getGenesRefClass(id, function(error, data2){
                    classgene = data2[0]['Class_Name'];
                    custom_gene['classgene'] = classgene;
                    callback2(custom_gene);
                });
            },
            ], function(custom_gene, err){
                list.push(custom_gene);
                console.log(list);
                callback1();
            });
        }
    }, function(err){
        // if any of the saves produced an error, err would equal that error
        if(err){
            console.log(list);
        }else{
            console.log(list);
        }
    });

最佳答案

您的代码有一些问题:

  • 它没有正确调用callback2()。它应该是callback2(null, custom_gene)(第一个参数是为错误保留的,如果没有错误则为null)。最好还应该检查 geneModel.getGenesRefClass() 返回的 error;
  • 上一问题还意味着您需要交换 function(custom_gene, err) 的参数(它应该变为 function(err, custom_gene));<
  • elem['Type_Gene']不等于"reference"时,您仍然应该调用callback1(),否则 async.each() 不知道代码已完成;

所以代码会变成这样:

var list = [];

async.each(data, function(elem, callback1) {
  var classgene   = '';
  var custom_gene = { Name_Gene : elem['Name_Gene'] };

  if (elem['Type_Gene'] == "reference") {
    async.waterfall([
      function(callback2) {
        var id = elem['Id_Genes'];
        geneModel.getGenesRefClass(id, function(error, data2){
          if (error) return callback2(error);
          classgene = data2[0]['Class_Name'];
          custom_gene['classgene'] = classgene;
          callback2(null, custom_gene);
        });
      },
    ], function(err, custom_gene) {
      // If you want to propagate errors, uncomment the following:
      // if (err) return callback1(err);
      list.push(custom_gene);
      console.log(list);
      callback1();
    });
  } else {
    callback1();
  }
}, function(err){
  // if any of the saves produced an error, err would equal that error
  if (err) {
    console.log('An error occurred!', err);
  }
  console.log(list);
});

关于javascript - 为什么 async.each Nodejs 不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37362972/

相关文章:

javascript - 变量未发布在 keyup 上

MySQL 时间戳查询

MySQL - 如果字段内有逗号,则 LOAD DATA LOCAL INFILE 不起作用?

node.js - 如何使用 passport 和 express 显示自定义错误消息

javascript - 将异步函数包装在 Promise 中

node.js - Sinon - 如何 stub 嵌套函数?

javascript - 警告 : flattenChildren(. ..) 与 react-native Navigator 和 DrawerLayoutAndroid

javascript - 我如何从 Angular 服务中获取数据

PHP PDO/从 MySQL 存储过程检索 OUT 参数

javascript - JS : What function will make my code DRY?