ml2008
ml2008

Reputation: 33

Retrieving data list from Firebase [Swift]

I'm trying to get a String array with the list of all usernames, but for some reason my code isn't working. Output should be [sean, yuh]

Database.database().reference().child("usernames").observeSingleEvent(of: .value, with: { (snapshot : DataSnapshot) in
        
  for child in snapshot.children {
     let snap = child as! DataSnapshot
            
     let uid = snap.childSnapshot(forPath: "username")
            
        self.array.append(uid1)
   }
}

enter image description here

Upvotes: 0

Views: 41

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

Your uid1 is not declared in the code you shared, and definitely not the value of the username property in the JSON. In addition, you'll want to get the value of the child snapshot. So combines:

Database.database().reference().child("usernames").observeSingleEvent(of: .value, with: { (snapshot : DataSnapshot) in
  for child in snapshot.children {
    let snap = child as! DataSnapshot
            
    let uid = snap.childSnapshot(forPath: "username")      
      self.array.append(uid.value)
    }
  }

Upvotes: 1

Related Questions