Swift Get Specific Value from Firebase Database

I am trying to get a specific value from a Firebase database. I looked through some of the documents like Google, but I couldn’t do this. Here is the database JSON file:

{
    "Kullanıcı" : {
        "ahmetozrahat25" : {
            "E-Mail" : "ahmetozrahat25@gmail.com",
            "Yetki" : "user"
        },
        "banuozrht" : {
            "E-Mail" : "banuozrahat@gmail.com",
            "Yetki" : "user"
        }
    }
}

Swift Code:

ref?.child("Kullanıcı").child(userName.text!).observeSingleEvent(of: .value, with: { (snapshot) in

    if let item = snapshot.value as? String{
        self.changedName = item
    }
})

I want to get a value E-Mailfor the user, not for everyone. How can i do this?

+4
source share
3 answers

I finally found a solution. If I declare var and try to use later, it returns nil, but if I try to use snapshot.value as? String, then this is normal. Here is an example I made.

    ref: FIRDatabaseReference?
    handle: FIRDatabaseHandle?

    let user = FIRAuth.auth()?.currentUser

    ref = FIRDatabase.database().reference()

    handle = ref?.child("Kullanıcı").child((user?.uid)!).child("Yetki").observe(.value, with: { (snapshot) in

        if let value = snapshot.value as? String{

            if snapshot.value as? String == "admin"{
                self.items.append("Soru Gönder")
                self.self.tblView.reloadData()
            }
        }
    })
+2
source

"E-Mail" .

ref?.child("Kullanıcı").child(userName.text!).child("E-Mail").observeSingleEvent(of: .value, with: { (snapshot) in

    if let item = snapshot.value as? String{
        self.changedName = item
    }
})
+1

In your code, the snapshot will contain a dictionary of child values. To access them, enter the value of the snapshot as a dictionary, and then access to individual children is a click (snapshot, lol)

ref?.child("Kullanıcı").child(userName.text!)
                       .observeSingleEvent(of: .value, with: { (snapshot) in

    let userDict = snapshot.value as! [String: Any]

    let email = userDict["E-Mail"] as! String
    let yetki = userDict["Yetki"] as! String
    print("email: \(email)  yetki: \(yetki)")
})
+1
source

Source: https://habr.com/ru/post/1669862/


All Articles