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