Promises with NodeJS

0

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

    
asked by Israel 26.11.2018 в 04:34
source

1 answer

0

Only as a preliminary note, in your function controlar_horario you have a parameter datosRes , but in the upper level that variable already exists, for which you are doing shadowing of the original variable. That is a bad practice but it does not explain the problem in question.

Let's cut to the chase. The error says that in the function

function controlar_horario(datosRes, res){

}

The datosRes parameter does not have a tiendas property, so evaluating datosRes.tiendas.length generates an error.

The error is triggered when in block catch you call a

}).catch(err => {
  controlar_horario(datosRes, res);
})

But if you entered the catch block, it is because there is a previous error that you are not logging or printing anywhere . I would start by finding out what it is that makes you enter the catch.

Knowing that there is an error above, this should be within one of the promises you entered in horarios or in what you do within then after Promise.all .

Looking at what you put in horarios I see that the definition of GetHorarios is:

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

At least I see that you pass a parameter tiendas and then you declare that var id_tienda = tienda[i].id; Could it be that the error is in not having used the plural?

I also have to comment that you are doing more laps than necessary. You could declare GetHorarios as:

function GetHorarios(tienda){
  ...
}

And forget about the index, which you would handle in the first loop doing:

for (var i =0; i < datosRes.tiendas.length; i++){
  horarios.push(GetHorarios(datosRes.tiendas[i]))
}
    
answered by 26.11.2018 в 12:31