0
votes

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

1
Totally off topic and I see there's an accepted answer but consider using the firebase users uid as their uid instead of rolling something custom. It makes like a lot simpler down the road.Jay

1 Answers

1
votes

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)
    }
  }