How to get id of data created in Firebase

Asked

Viewed 259 times

1

To better illustrate... in my database I have a collection that stores the section of users.

Imagem da arvore de dados no firebase

After creating the user my Javascript code adds one more user to the "users" collection and also an item in "Session" that stores the user id.

My question is: how to get this code firebase automatically assigned to sesion items'?

Formulário de cadastro

Follow the javascript code:

/* Pegar dados do usuario */
nome = document.querySelector('#username');
sobrenome = document.querySelector('#sobrenome');
createUserButton = document.querySelector('#submit');
email = document.querySelector('#email');
dataNascimento = document.querySelector('#data');
senha = document.querySelector('#senha');
confirm_senha = document.querySelector('#confirm-senha');
concordaTermos = document.querySelector('#check');

/*Cria evento do botão para enviar dados de login*/
createUserButton.addEventListener('click', function() {

    /* Confere se o usuario concorda com os termos de uso */
    if (concordaTermos.checked) {

        /*cria o usuario*/
        firebase
            .auth()
            .createUserWithEmailAndPassword(email.value, senha.value)
            .then(function() {
                /* Armazena dados no banco de dados */
                firebase.database()
                    .ref('users/' + firebase.auth().currentUser.uid)
                    .set({
                        'nome': nome.value,
                        'sobrenome': sobrenome.value,
                        'email': email.value,
                        'dataNascimento': dataNascimento.value

                    })
                    .then(function() {

                        /* Armazena seção do usuario */
                        firebase
                            .database()
                            .ref('sesion/')
                            .push()
                            .set(firebase.auth().currentUser.uid);


                        console.log(secion);
                    })
            })

    }

})
  • Hi Kawan! Welcome to our beloved community! I’ve been away for a while. Cool your question and it’s not hard to solve (if you do, post an answer to your own question! - That’s pretty cool, you know? Someone might have a similar question). I made an edit just to fix some terms. As a suggestion, use the correct term for "Session" to represent time in your collection. Another suggestion, do you really need the Realtime database? Also, make sure Firestore doesn’t solve your problem. Know the differences by reading the documentation.

  • Object.Keys(firebase.auth().currentUser.uid)[0] wouldn’t solve the problem?

1 answer

0


when you create uses the push() method in a database reference it returns a new reference that has a . key field soon

const sessioRef = firebase.database().ref('sesion').push();
const codigo = sessionRef.key //use para o que quiser.
sessionRef.set(firebase.auth().currentUser.uid);

Browser other questions tagged

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