Error saving data in firebase

0

I have a function that stores a JSON object as a parameter and breaks it down into two new objects:

function insertDB(result){

var identification = "cadena";


for (var i = 0; i <= result.length; i++) {

while(result[i].Identificacion != identification ){

  db.ref('Estudiantes/'+ result[i].people_code_id).set({
    nombre: result[i].Nombres,
    identificacion: result[i].Identificacion,
    programa : result[i].ProgramaMateria,
    direccion : result[i].DIRECCION,
    ciudad: result[i].CIUDAD,
    departamento: result[i].DEPARTAMENTO,
    telFijo: result[i].TelFijo,
    telMovil: result[i].TelMovil,
    peopleCode: result[i].people_code_id,
    correo: result[i].EMAIL,
  });
  identification = result[i].Identificacion;
}
}

var CodigoMateria = 0;

for (var i = 0; i <= result.length; i++) {

  while(result[i].CodigoMateria != CodigoMateria){
    db.ref('Materias/'+ result[i].CodigoMateria).set({
      CodMateria : result[i].CodigoMateria,
      Nombre: result[i].NombreMateria,
      Facultad: result[i].Facultad,
      Semestre: result[i].Semestre
    });
    CodigoMateria = result[i].CodigoMateria;
  }
}
}

The problem is that it only saves the first JSON object (Students) in the BD (FIREBASE) and it does not generate the second one, I have tested the methods separately and they both send, but putting them together so that the two objects are sent does not work .

    
asked by DVertel 12.12.2017 в 20:46
source

1 answer

0

You have errors in the for . I understand that the data you send has the following structure:

var datos = [
{
    Identificacion: "Cadena",
    CodigoMateria: 0,
    //...
},{
    Identificacion: "Cadena 2",
    CodigoMateria: 1,
    //...
}
]

The problem is in the ' for ' path, you start at i = 0 and you have to go to i result.length and not i < = result.length

The for should look like this:

for (var i = 0; i < result.length; i++) {
    //Aqui va tu codigo...
}
    
answered by 13.12.2017 / 19:48
source