Zing
Zing

Reputation: 41

Finding userID on Firebase

I saw this code on Firebase tutorial and I dont know what they mean by writing "userID".(if there is a data that I need to take, where should I can find this?

function writeUserData(userId, name, email, imageUrl) {
  firebase.database().ref('users/' + userId).set({
    username: name,
    email: email,
    profile_picture : imageUrl
  });
}

Upvotes: 0

Views: 39

Answers (1)

DANIEL SSEJJEMBA
DANIEL SSEJJEMBA

Reputation: 395

I don't know if I correctly understand your question, but each user in firebase is assigned a unique identification string called the uid which is the userId in this case. This uniquely tags the user in the database and any data stored and is uniquely attached to that user can be stored under the uid node for easy reference. What I am seeing here with the function writeUserData() is implemented to be called after registering a user. So you can get the uid by simply calling firebase.auth().currentUser.uid. This will return the string that you can provide as the userID. A cleaner way could be to first make sure the current user is not null forexample:

var user = firebase.auth().currentUser
var userID = ''

if(user !== null) {
 userID = user.uid
} else{
 console.warn("User does not exist")
}

Upvotes: 1

Related Questions