简体   繁体   中英

why is not working properly async.each nodejs?

I'm trying to use async.each function to get an array with my results from two queries. After that, I need to render this results in a web page.

The async.each function calcule the variable results properly, but, I am not be able to export this variable outside the function and render it and I don't understand why.

Here I attached the code, where I tested it. I realized that when I call "callback1" the function(error) is not working and I don't get the variable list in the console (so I won't be able to render it later on). Please I would be grateful if someone could help me with that. Thanks a lot.

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

Your code has a few problems:

  • It's not calling callback2() properly. It should be callback2(null, custom_gene) (the first argument is reserved for errors, or null if there aren't any). Preferably, you should also check for error being returned by geneModel.getGenesRefClass() ;
  • The previous issue also means that you need to swap the argument of function(custom_gene, err) (it should become function(err, custom_gene) );
  • When elem['Type_Gene'] does not equal "reference" , you should still call callback1() , otherwise async.each() doesn't know that the code is done;

So the code would become something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM