从Firebase中提取特定用户

时间:2018-01-13 14:11:52

标签: swift firebase

我已成功设法从Firebase为所有用户提取数据,但是在尝试根据userId为特定用户提取数据时,我感到很挣扎。

我已成功将userId传递给该函数。我用print(userId)对此进行了测试。

似乎userdataSnapshot包含正确的数据,但是用户数据快照中的用户数为#34;循环遍历用户的所有孩子。第一个用于" firstName",然后" lastName" ..而不是将所有这些值都放入数组中..感觉错误与for循环有关。

查看代码后的输出。

// Get all data about a specific user
    func getUserdata(userId: String, handler: @escaping (_ userdata: [User]) -> ()) {
        print("UserId")
        print(userId)
        var userArray = [User]()
        REF_USERS.child(userId).observeSingleEvent(of: .value) { (userdataSnapshot) in
            guard let userdataSnapshot = userdataSnapshot.children.allObjects as? [DataSnapshot] else { return }
            print("DataService")
            dump(userdataSnapshot)

            for user in userdataSnapshot {
                let userId = userId                                                                     // pull content from Firebase
                let firstName = user.childSnapshot(forPath: "firstName").value as? String ?? ""               // pull content from Firebase
                let lastName = user.childSnapshot(forPath: "lastName").value as? String ?? ""                 // pull content from Firebase
                let name = user.childSnapshot(forPath: "name").value as? String ?? ""                         // pull content from Firebase
                let city = user.childSnapshot(forPath: "city").value as? String ?? ""                        // pull content from Firebase
                let country = user.childSnapshot(forPath: "country").value as? String ?? ""                   // pull content from Firebase
                let profileImageURL = user.childSnapshot(forPath: "profileImageURL").value as? String ?? ""  // pull content from Firebase
                let defaultHunt = user.childSnapshot(forPath: "defaultHunt").value as? String ?? ""          // pull content from Firebase

                let user = User(userId: userId, firstName: firstName, lastName: lastName, name: name, city: city, country: country, profileImageURL: profileImageURL, defaultHunt: defaultHunt)
                userArray.append(user)
            }
            //print("DataService array content test")
           // dump(userArray)
            handler(userArray)
        }
    }

终端输出

UserId
NhZZGwJQCGe2OGaNTwGvpPuQKNA2
DataService
▿ 9 elements
  - Snap (city) Oslo #0
    - super: NSObject
  - Snap (country) Norway #1
    - super: NSObject
  - Snap (defaultHunt) Hjortejakt #2
    - super: NSObject
  - Snap (email) christian.simonsen@gmail.com #3
    - super: NSObject
  - Snap (firstName) Christian #4
    - super: NSObject
  - Snap (lastName) Simonsen #5
    - super: NSObject
  - Snap (name) Christian #6
    - super: NSObject
  - Snap (profileImageURL) https://firebasestorage.googleapis.com/v0/b/shoota-179610.appspot.com/o/gghh%2F985188F8-1439-4906-8BDA-497EAEBDD99A?alt=media&token=359fa0b0-6a90-4393-ab11-a354437f701d #7
    - super: NSObject
  - Snap (provider) Firebase #8
    - super: NSObject
userArray
▿ 9 elements
  ▿ Shoota.User #0
    ▿ userId: NhZZGwJQCGe2OGaNTwGvpPuQKNA2
      - some: "NhZZGwJQCGe2OGaNTwGvpPuQKNA2"
    - firstName: ""
    - lastName: ""
    - name: ""
    - city: ""
    - country: ""
    - profileImageURL: ""
    - defaultHunt: ""
  ▿ Shoota.User #1
    ▿ userId: NhZZGwJQCGe2OGaNTwGvpPuQKNA2
      - some: "NhZZGwJQCGe2OGaNTwGvpPuQKNA2"
    - firstName: ""
    - lastName: ""
    - name: ""
    - city: ""
    - country: ""
    - profileImageURL: ""
    - defaultHunt: ""
  ▿ Shoota.User #2
    ▿ userId: NhZZGwJQCGe2OGaNTwGvpPuQKNA2
      - some: "NhZZGwJQCGe2OGaNTwGvpPuQKNA2"
    - firstName: ""
    - lastName: ""
    - name: ""
    - city: ""
    - country: ""
    - profileImageURL: ""
    - defaultHunt: ""
  ▿ Shoota.User #3
    ▿ userId: NhZZGwJQCGe2OGaNTwGvpPuQKNA2
      - some: "NhZZGwJQCGe2OGaNTwGvpPuQKNA2"
    - firstName: ""
    - lastName: ""
    - name: ""
    - city: ""
    - country: ""
    - profileImageURL: ""
    - defaultHunt: ""
  ▿ Shoota.User #4
    ▿ userId: NhZZGwJQCGe2OGaNTwGvpPuQKNA2
      - some: "NhZZGwJQCGe2OGaNTwGvpPuQKNA2"
    - firstName: ""
    - lastName: ""
    - name: ""
    - city: ""
    - country: ""
    - profileImageURL: ""
    - defaultHunt: ""
  ▿ Shoota.User #5
    ▿ userId: NhZZGwJQCGe2OGaNTwGvpPuQKNA2
      - some: "NhZZGwJQCGe2OGaNTwGvpPuQKNA2"
    - firstName: ""
    - lastName: ""
    - name: ""
    - city: ""
    - country: ""
    - profileImageURL: ""
    - defaultHunt: ""
  ▿ Shoota.User #6
    ▿ userId: NhZZGwJQCGe2OGaNTwGvpPuQKNA2
      - some: "NhZZGwJQCGe2OGaNTwGvpPuQKNA2"
    - firstName: ""
    - lastName: ""
    - name: ""
    - city: ""
    - country: ""
    - profileImageURL: ""
    - defaultHunt: ""
  ▿ Shoota.User #7
    ▿ userId: NhZZGwJQCGe2OGaNTwGvpPuQKNA2
      - some: "NhZZGwJQCGe2OGaNTwGvpPuQKNA2"
    - firstName: ""
    - lastName: ""
    - name: ""
    - city: ""
    - country: ""
    - profileImageURL: ""
    - defaultHunt: ""
  ▿ Shoota.User #8
    ▿ userId: NhZZGwJQCGe2OGaNTwGvpPuQKNA2
      - some: "NhZZGwJQCGe2OGaNTwGvpPuQKNA2"
    - firstName: ""
    - lastName: ""
    - name: ""
    - city: ""
    - country: ""
    - profileImageURL: ""
    - defaultHunt: ""

2 个答案:

答案 0 :(得分:0)

为什么不能使用:

    let query = ref.queryOrdered(byChild: "userId").queryEqual(toValue: (userId))
    query.observe(.childAdded, with: { snapshot in...

答案 1 :(得分:0)

我最终解决了它。以下是更新的代码。基本上错误与守卫让

有关

重新阅读Firebase文档后我解决了这个问题。请参阅此page

上的“一次读取数据”部分
// Get all data about a specific user 
func getUserdata(userId: String, handler: @escaping (_ userdata: [User]) -> ()) {
    var userArray = [User]()
    REF_USERS.child(userId).observeSingleEvent(of: .value) { (snapshot) in
        guard let data = snapshot.value as? NSDictionary else { return }

        let userId = userId
        let firstName = data["firstName"] as? String ?? ""
        let lastName = data["lastName"] as? String ?? ""
        let name = data["name"] as? String ?? ""
        let city = data["city"] as? String ?? ""
        let country = data["country"] as? String ?? ""
        let profileImageURL = data["profileImageURL"] as? String ?? ""
        let defaultHunt = data["defaultHunt"] as? String ?? ""

        let user = User(userId: userId, firstName: firstName, lastName: lastName, name: name, city: city, country: country, profileImageURL: profileImageURL, defaultHunt: defaultHunt)

        userArray.append(user)
        handler(userArray)
    }
}