Firebase Getting Data in Swift

I am trying to get specific data only from the current user. My data in my database is as follows:

enter image description here

For example, I want to just grab the full_name and save it in the userName variable. Below I use to capture my data.

ref.queryOrderedByChild("full_name").queryEqualToValue("userIdentifier").observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in print(snapshot.value) // let userName = snapshot.value["full_name"] as! String }) 

Unfortunately, this is what my console prints.

enter image description here

I would be grateful for any help :) Thank you!

+6
source share
5 answers

It gives you this warning message indexOn because you are executing a request.

You must specify the keys that you will index using .indexOn in the security rules and fire base. As long as you are allowed to create these ad-hoc requests on the client, you will see that improved performance when using .indexOn

As you know, the name you are looking for, you can directly go to this node without asking.

  let ref:FIRDatabaseReference! // your ref ie. root.child("users").child(" stephenwarren001@yahoo.com ") // only need to fetch once so use single event ref.observeSingleEventOfType(.Value, withBlock: { snapshot in if !snapshot.exists() { return } //print(snapshot) if let userName = snapshot.value["full_name"] as? String { print(userName) } if let email = snapshot.value["email"] as? String { print(email) } // can also use // snapshot.childSnapshotForPath("full_name").value as! String }) 
+10
source
 { "rules": { "tbl_name": { ".indexOn": ["field_name1", "field_name2"] }, ".read": true, ".write": true } } 

You can apply indexOn in any field. Add this json to the Security & Rules Rules tab. Hope this works for you. :)

+2
source

It retrieves the registered user data:

 let ref = FIRDatabase.database().reference(fromURL: "DATABASE_URl") let userID = FIRAuth.auth()?.currentUser?.uid let usersRef = ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in print(snapshot) 
+2
source

Swift 4

 let ref = Database.database().reference(withPath: "user") ref.observeSingleEvent(of: .value, with: { snapshot in if !snapshot.exists() { return } print(snapshot) // Its print all values including Snap (User) print(snapshot.value!) let username = snapshot.childSnapshot(forPath: "full_name").value print(username!) }) 
+1
source
 let ref = Database.database().reference().child("users/ stephenwarren001@yahoo.com ") ref.observeSingleEvent(of: .value, with: { (snap : DataSnapshot) in print("\(String(describing: snap.value))") }) { (err: Error) in print("\(err.localizedDescription)") } 
0
source

All Articles