I have an array of stores which by promises I try to obtain the schedule of each, but in some cases I get the following error:
UnhandledPromiseRejectionWarning: TypeError: Can not read property 'length' of undefined at control_home (line code 452: 41) at Promise.all.then.catch.err (line code 426: 15)
This is my code:
for (var i =0; i < datosRes.tiendas.length; i++){
horarios.push(GetHorarios(datosRes.tiendas, i))
}
Promise.all(horarios).then(response => {
for(var i=0;i<response.length;i++){
if(response[i].length > 0){
datosRes.tiendas[i].hay_horario = true;
for(var j=0;j<response[i].length;j++){
if(datosRes.tiendas[i].id == response[i][j].id_tienda_horario){
datosRes.tiendas[i].listaHorarios = response[i][j];
}
}
}else{
datosRes.tiendas[i].hay_horario = false;
}
}
eq.local = data;
}).catch(err => {
controlar_horario(datosRes, res)//esta es la linea 426
})
function GetHorarios(tiendas, i){
return new Promise(function(resolve, reject){
var hoy = moment().format("d")
var id_tienda = tienda[i].id;
bd_getHorario.getHorarioPorDia(id_tienda, hoy,function(error, data){
if(error || data.error){
errorDB = {"error_log": error, "error_data": data.error};
reject(errorDB)
}else{
resolve(data)
}
})
})
}
function controlar_horario(datosRes, res){
for(var i =0; i < datosRes.tiendas.length; i++){ //esta es la linea 452
if(datosRes.tiendas[i].hay_horario){
//controlar horarios .....
}else{
//no hay horario .....
}
return res.json(200,{"datos":datosRes});
}
}
I really do not know why you give me that error, I would be grateful for your help