0 votes

As save the same ID that is generated when you push on a property of the object in firebase?

Currently I have a node called 'releases', which contains the following object:

introducir la descripción de la imagen aquí

As I do so that the same key that is generated by push of the firebase in that object is "L_E401l0yZ...." save it in a property of that object, so that it is like this:

-L_E401I0yZmGMn12L07{
   fecha_creacion: "2019-3-5"
   mensaje: "mensaje del primer comunicado"
   titulo: "primer comunicado"
   id: "L_E401I0yZmGMn12L07"
}

What I have done in the following way, but I have not had results:

crearComunicado(comunicado){
        let vm = this;
        let fecha_actual = new Date()
        let anio = fecha_actual.getFullYear();
        let mes  = fecha_actual.getMonth()+ 1;
        let dia  = fecha_actual.getDate();
        let hoy = anio + "-"+mes+"-"+dia;

        var myRef = firebase.database().ref().push();
        var key = myRef.key();

      console.log("KEY");
      console.log(key);
        firebase
          .database()
          .ref('comunicados')
          .push({
            titulo: comunicado.titulo,
            mensaje: comunicado.mensaje,
            fecha_creacion: hoy,
            id: key
          })
          .then(()=>{
            console.log("Comunicado creado exitosamente");
            vm.dialogCrear = false;
          })
      }

1voto

Gastón Saillén Points 1822

Where returns a promise you can send the object with the value of your key

   .then((snapshot)=>{
                myRef.child(snapshot.key).update({"id": snapshot.key})
                console.log("Comunicado creado exitosamente.");
                vm.dialogCrear = false;
              })

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X