Firebase request in Swift

I have such a structure in "/ users"

{
 "aaa@aaa-com":{
     "email":"aaa@aaa.com",
     "name": "Joseph",
 },
 "asd@asd-com": {
     "email": "asd@asd.com",
     "name": "Erik"
 }
}

I made a request for access to a child who has a letter " aaa@aaa.com ":

    usersRef.queryOrderedByChild("email").queryEqualToValue("aaa@aaa.com") .observeEventType(.Value, withBlock: { snapshot in
        if snapshot.exists() {
            print("user exists")

        } else if !snapshot.exists(){
            print("user doesn't exists")


        } 

If I print (snapshot), I get console output in xcode:

Snap (users) {
    "aaa@aaa-com" =     {
         email = "aaa@aaa.com";
         name = "Joseph";
     };
 }

I am trying to find a way to get the username after executing this request, but I have not found a way to do this. I tried:

    print(snapshot.value.objectForKey("name"))
    print(snapshot.value.valueForKey("name"))

but I get zero in both.

Do you know how to get the name value?

Thanks in advance!

+4
source share
3 answers

This question is old, and there are tons of other resources. Hope this helps if you haven't received it yet.

- snapshot.value [String: AnyObject]. .

usersRef.queryOrderedByChild("email")
        .queryEqualToValue("aaa@aaa.com")
        .observeEventType(.Value, withBlock: { snapshot in

    // if you know that your value will not be nil, then you can unwrap like below
    //let foo = snapshot.value as! [String: AnyObject]

    if let foo = snapshot.value as? [String: AnyObject] {
        let name = foo["name"] as? String
        let email = foo["email"] as? String
    }

} 

Swift 3

usersRef.queryOrdered(byChild: "email")
        .queryEqual(toValue: "aaa@aaa.com")
        .observe(.value, with: { snapshot in

    // if you know that your value will not be nil, then you can unwrap like below
    //let foo = snapshot.value as! [String: AnyObject]

    if let foo = snapshot.value as? [String: AnyObject] {
        let name = foo["name"] as? String
        let email = foo["email"] as? String
    }

}

JSON - , .

users node , :

usersRef.observe(.value, with: { snapshot in

    let users: [User] = []

    for item in snapshot.children {

        if let foo = item.value as? [String: AnyObject] {
            let user = User()

            user.name = foo["name"] as? String
            user.email = foo["email"] as? String

            users.append(user)
        }

    }

}
+2

NSDictionary,

snap.value! as! NSDictionary

, nil

0

Try the following:

var ref = new Firebase("https://YOUR-URL.firebaseio.com/");
ref.orderByChild("location").equalTo("United States").on("child_added", function(snapshot) {
  document.write(snapshot.key());
console.log(snapshot.key());
});

Replace locationwith emailor nameand the United Satesparameter you are looking for.

BTW document.write(snapshot.key())actually prints snapshoton a web page HTML, not just a console!

-3
source

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


All Articles