problems when updating from an array in mongo and nodejs using async

0

Hi, I have a problem I want to update, a database from an array that I have, to cycle the array and every key that exists in the db that I update ... I have written the code, but I only update the last record .. I do not know why.

   var actualiza=function(row){
                                // console.log('-------------------')
           console.log(row.key)
              // console.log('-------------------')
            Locations.tenant('test').update({key:row.key},{$set:row}).exec(function(error, resp){
                if(!error){
                     for(var e=0; e<resp.length; e++){
                        console.log('------------------------------')
                        console.log(resp[e].key)
                        }
                       }else{
                       console.log(error)
                        }
                    })
          }
   
   
Locations.tenant('test').find().exec(function(e,r){ //query mongo
                  datos_db=r; //datos de la base de datos    
                        async.auto({
                                //recorrer para hacer la comparacion de los key de la db con las del excel
                                  bd: function(callback){
                                    for(var i=0; i<datos_db.length; i++){
                                      for(var j=0; j<result.length; j++){
                                        if(result[j].key==datos_db[i].key){ //comparar cuales key son iguales
                                            nuevo.push(result[j]);
                                        }
                                      }
                                    }
                                // console.log(nuevo) //nuevo: ese le array de los key existentes
                                    callback(null, nuevo)
                                  },
                                  fill:function(callback){
                                    async.each(nuevo, function(item, next) {
                                      // console.log(item.key)
                                      promesas.push(actualiza(item)) //function update
                                      next();
                                    }, function(err){
                                      if( !err ) {
                                        callback(null, promesas)
                                      } else {
                                        callback(err, null)
                                      }
                                    });
                                  },
                                  updateBd: function(callback){
                                    Promise.all(promesas)
                                    .then(function(data){
                                      callback(null, data)
                                    })
                                    .catch(function(err){
                                      callback(err, null)
                                    })
                                  }
                                }, 
                                function(err, res){
                                  if(!err){
                                    console.log(res);
                                  }else{
                                    console.log(err);
                                  }
                        }
                        )

             })
    
asked by Valentin Antonio Bautista 16.08.2016 в 17:55
source

1 answer

0

var asyncLoop     = require('node-async-loop');

asyncLoop(nuevo, function (item, next){ 
      var promesas  = [];
       setTimeout(function(data){
           promesas.push(actualiza(item))
             next();
      }, 500);
    }
  );
    
answered by 19.08.2016 / 18:10
source