4

I have an structure like this in "/users"

{ "aaa@aaa-com":{ "email":"[email protected]", "name": "Joseph", }, "asd@asd-com": { "email": "[email protected]", "name": "Erik" } } 

I made a query to access the child that has the email "[email protected]" :

 usersRef.queryOrderedByChild("email").queryEqualToValue("[email protected]") .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 the console output in xcode:

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

I'm trying to find some way to get the name of the user after doing this query, but I didn't find the way to do it. I tried with:

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

but I get nil in both.

Do you guys know how to get the name value?

Thanks in advance!!

1
  • 1
    Have you tried instead of quering ("aaa@aaa-com") instead of ("[email protected]")? I think that's why is not finding it Commented Feb 26, 2016 at 15:21

3 Answers 3

2

This question is old, and there are a ton of other resources out there. Hopefully this will help if you haven't gotten there already.

The easiest way is to cast snapshot.value to type [String: AnyObject]. Then, you can access the inner nodes.

usersRef.queryOrderedByChild("email") .queryEqualToValue("[email protected]") .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: "[email protected]") .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 } } 

Given your position in the JSON tree because of your query, the above should work for you.

If you were one level up in the tree at the users node, and wanted to get all values, you could access like this:

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) } } } 
Sign up to request clarification or add additional context in comments.

Comments

0

try to convert the snapshot into a NSDictionary like this

snap.value! as! NSDictionary 

make sure that the snapshot isn´t nil

Comments

-3

Try this:

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 location with email, or name, and United Sates with a parameter that you are searching for.

BTW, document.write(snapshot.key()) will actually print the snapshot onto the HTML webpage instead of just in the console!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.