john doe
john doe

Reputation: 9660

Fetching Firebase Records Based on Email

I am trying to fetch all the driveways which belongs to user using their email as the search key.

enter image description here

And here is the code I am writing:

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

How can I get all the driveways based on user's email address?

Upvotes: 0

Views: 142

Answers (1)

Jay
Jay

Reputation: 35658

Try this (Swift 3 Firebase 3)

let email = "[email protected]"
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)")
    }
})

Upvotes: 2

Related Questions