Get real-time id Firebase

0

I am creating a new document in firebase and like to get the id that I will be creating to save in my collection, however how would I get the same if I have not yet saved it in the database?

Example: the id that will generate will be Iq313NGP4V6jrjdlWn4T, I would like to be able to save this in a variable and pass it 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

                })
    
asked by anonymous 25.09.2018 / 00:59

1 answer

0

Try 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
  });
    
25.09.2018 / 18:57