正在从 Firebase 检索数据列表 [Swift]

Retrieving data list from Firebase [Swift]

我正在尝试获取包含所有用户名列表的字符串数组,但出于某种原因我的代码无法正常工作。输出应该是 [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)
   }
}

您的 uid1 未在您共享的代码中声明,并且绝对不是 JSON 中用户名 属性 的值。此外,您还需要获取子快照的 value。所以合并:

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