vickydutta
vickydutta

Reputation: 3

How to get user specific data in Firebase Swift 3

I want to retrieve data of current user in the application. How can I do that. ?

Here is my current user's firebase id :

enter image description here

And my table node is :

enter image description here

Here it is clear that my uid and nodes title are different. So how can I get particular user's data.

My code till now:

self.ref =  Database.database().reference()
let userID = Auth.auth().currentUser?.uid
self.ref.child("member").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
 if !snapshot.exists() {
                    // handle data not found
                    return
 }
})

Upvotes: 0

Views: 563

Answers (2)

Xcodian Solangi
Xcodian Solangi

Reputation: 2408

You are getting different IDs, because the first one Wnlxl... is the userID and the other -KVvR664... is an autoID.
You have set .childByAutoID following the member, if you want to retrieve the data of the current user then, you have to put userID following member.
Then you can easily retrieve the user's data by userID because each and every detail will be saved under userID.
Hope this code will work for you.

let ref : FIRDatabaseReference!
ref = FIRDatabase.database().reference()    
ref.child("member").child(user!.uid).setValue(["name": self.fullName.text!, "email": self.email.text!, "mobile": self.mobile.text!, "doj": self.doj.text!])

Upvotes: 1

Torewin
Torewin

Reputation: 907

Your Firebase screenshot is incorrect I think - your code will work fine, but how you create these members are wrong.

When creating the members you are saying .childByAutoID which you should be using Auth.auth().currentUser?.uid. If that makes sense. Let me know.

Edit: to create a user with userUID data in the node member do the following:

if let userUID = Auth().auth.currentUser?.uid{
   ref.child("members/\(userUID)").setValue(*YOURMEMBERINFODICTIONARY*)
}

Upvotes: 0

Related Questions