根据电子邮件获取Firebase记录

时间:2017-01-28 14:50:22

标签: ios swift firebase firebase-realtime-database

我正在尝试使用他们的电子邮件作为搜索键来获取属于用户的所有车道。

enter image description here

这是我写的代码:

  guard let currentUser = FIRAuth.auth()?.currentUser else {
            return
        }

        let query = FIRDatabase.database().reference(withPath :"driveways").queryEqual(toValue: currentUser.email!, childKey: "email")

        query.observe(.value, with: { (snapshot) in
            print(snapshot)
        })

如何根据用户的电子邮件地址获取所有车道?

1 个答案:

答案 0 :(得分:2)

试试这个(Swift 3 Firebase 3)

let email = "johndoe@gmail.com"
let queryRef = drivewaysRef.queryOrdered(byChild: "email")
                           .queryEqual(toValue: email)
queryRef.observeSingleEvent(of: .value, with: { snapshot in
    for snap in snapshot.children {
        let driveSnap = snap as! FIRDataSnapshot
        let driveDict = driveSnap.value as! [String:AnyObject] //driveway child data
        let city = driveDict["city"] as! String
        let state = driveDict["state"] as! String
        print("email: \(email)  city: \(city)  state: \(state)")
    }
})