Catch real-time id Firebase

Asked

Viewed 411 times

0

I’m creating a new document in firebase and likes to take the id I’ll be creating to save in my collection, but how would I get it if I haven’t saved it in the database?

Example: the id you will generate will be Iq313ngp4v6jrjdlwn4t, I would like to be able to save this in a variable and go to cd_id in . set.

var novoProduto = db.collection("/caminho").doc();

                novoProduto.set({
                    cd_categoria: categoria,
                    cd_id: 'aqui gostaria de ter o id',
                    ds_descricao: descricao,
                    ds_nome: nome,
                    modalidade: "1",
                    nr_preco: preco

                })

1 answer

0


Try it like this:


  var novo_id = firebase.database().ref().child('caminho').push().key;

  firebase.database().ref('caminho/' + novo_id).set({
         cd_categoria: categoria,
         cd_id: novo_id,
         ds_descricao: descricao,
         ds_nome: nome,
         modalidade: "1",
         nr_preco: preco
  });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.