Using the push () method to create a new child location without a unique key

In my firebase web application, when new users register with their email, I create a node under my email address under the root ref

But the problem is to use the push () method to add each new user to the database, each user is created using a unique key, as shown below

- users

---- ASFTU578FE

--------- user: user1@email.com

---- FDWWE36S46

--------- user: user2@email.com

---- WERSRTT23W

--------- user: user3@email.com

Now, how can I access the path for the user, since I do not know the unque key that will be created for each new user.

Is there a way to push a new user without a unique key, but a key that I know as user.displayName or user.email

+4
source share
1 answer

A call pushcreates a location using an automatically calculated key.

To record a child along a path that you define yourself, you simply call child("key").set(value).

If you store Firebase Authentication users in your database, the idiomatic way is to save them under their uid.

var user = firebase.auth().currentUser;
var usersRef = firebase.database().ref("users");
if (user) {
  usersRef.child(user.uid).set({ 
    displayName: displayName,
    email: email,
    photoUrl: photoUrl,
    emailVerified: emailVerified
  });
}
+11
source

Source: https://habr.com/ru/post/1672666/


All Articles