Equal IDs for user registration by email method when doing push in Firebase

0

My question is how to do so that the identifier of a user record is the same in email and when saving data is by the same identifier all this I am doing in html and javascript the code is as follows:

var database = firebase.database();
var userConect = null;
var  Nombre = document.getElementById('nombre').value;
var  Apellido = document.getElementById('apellido').value;
var  Edad = document.getElementById('edad').value;

function registro(){
firebase.auth().createUserWithEmailAndPassword(email, password)
.then(function() {
console.log('OK');
})

So here is the code to create the account by email

This part is the listener

function observador() {
  firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
  console.log('Ya hay un usuario activo');
  // User is signed in.
  var displayName = user.displayName;
  var email = user.email;
  console.log(user.emailVerified);
  var emailVerified = user.emailVerified;
  var uid = user.uid; 
  userConect = database.ref("users");
  AgregarDB(user.uid, user.emailVerified, Nombre, Apellido, Edad);
    function AgregarDB(Nombre,Apellido, Edad) {
      var  Nombre = document.getElementById('nombre').value;
      var  Apellido = document.getElementById('apellido').value;
      var  Edad = document.getElementById('edad').value;

      var conectados = userConect.push({
      uid: uid,
      emailVerified: emailVerified,
      Nombre: Nombre,
      Apellido: Apellido,
      Edad: Edad
  });
}
}

All this is the code and insert images for a better understanding of my question

image 1

image 2

I want the key image of the push (DB) to be the same as image 1 (user registration by email) key of the user record

    
asked by Francisco Navarrete 13.07.2018 в 17:05
source

2 answers

1

Good morning.

All you need to know is in the documentation . Although I do not blame you because you do not explicitly mention the child() that have the references of the database.

The documentation in English explains:

  

Returns a Reference for location on the relative path   specified.

     

The relative path can be a simple child name ("ada") or a path   Deeper separated by plecas ("ada / name / firts").

The best translation of my life B)

In other words what it means is that child(ruta) works just like when you do database.ref(ruta) just that it's relative. That is,

  • .ref() uses the path from the root of the database
  • .child() uses the path from Reference .

In any case, you can play with this. To achieve what you need, you can do several things, all valid:

userConect = database.ref("users");
userReferencia = database.ref("users").child(uid); // Usando child
userReferencia = database.ref("users/" + uid); // Usando la ruta profunda

userReferencia = userConect.child(uid); // Usando un Reference ya existente
userReferencia = database.ref().child("users").child("uid"); // También podés usar child en child

This last one can be useful because for reasons of order you can have the Reference of your users and the Reference of your specific user separately. Although it is the same.

And one last note is that when you have your userReference do not do push() but set or eventually update when you already have info inside. This is because push will create a deeper node in the database. While set will leave your data where you need it. Use it and you'll see what I mean.

    
answered by 13.07.2018 / 18:32
source
0

userConect = database.ref ("users"). child (uid); This is the answer

    
answered by 13.07.2018 в 18:20