0

I have 2 questions relating to this query:

  1. I'm running a query in Firebase, the query returns the result I'm looking for but when I try to access the data in the query, it is nil

  2. How can I access the tag key in the query? It is 1 deep

Thank you in advance

Query

ref.child("users")
   .queryOrderedByChild("receivePostRequest/status")
   .queryEqualToValue(true)
   .observeEventType(.Value, withBlock: {snapshot in

JSON result

Optional({
    lgmSZ1HnMnSzE71kCLfdxK8AN2G2 =     {
        age = 18;
        email = "lon1@gmail.com";
        firstname = Jamie;
        lastname = lon;
        latitude = "37.3325232";
        longitude = "-122.0286527";
        profilePic = "users/profilePhoto/W6pK2HHA1TZC9wicnCaODQhHvoi1.jpg";
        receivePostRequest =         {
            status = 1;
            tag = tagSample;

        };
        userId = lgmSZ1HnMnSzE71kCLfdxK8AN2G2;
    };
})

This is the complete query

ref.child("users")
   .queryOrderedByChild("receivePostRequest/status")
   .queryEqualToValue(true)
   .observeEventType(.Value, withBlock: {snapshot in
             print(snapshot.value["firstname"])
        })

JSON

{
  "posts" : {
    "-KJGom2RvmWkfbvsFXij" : {
      "postAddress" : "Post Address",
      "postCompletionAddress" : "post completion address",
      "postCreationTime" : "postCreation Time",
      "postDateTime" : "Date and Time",
      "postDescription" : "Post Detail",
      "postPay" : "post Pay",
      "postTitle" : "Post Title",
      "status" : "pending",
      "userAiD" : "W6pK2HHA1TZC9wicnCaODQhHvoi1",
      "userBiD" : "lgmSZ1HnMnSzE71kCLfdxK8AN2G2"
    },
  },
  "users" : {
    "W6pK2HHA1TZC9wicnCaODQhHvoi1" : {
      "age" : 18,
      "email" : "ama@gmail.com",
      "firstname" : "jam",
      "lastname" : "lime",
      "latitude" : 37.332172,
      "longitude" : -122.035089,
      "profilePic" : "users/profilePhoto/W6pK2HHA1TZC9wicnCaODQhHvoi1.jpg",
      "receivePostRequest" : {
        "lat" : 28.10277584477151,
        "latitude" : 37.33067237,
        "long" : -81.4587166999294,
        "longitud" : -122.03014382,
        "status" : false,
        "tags" : {
          "tag1" : 444
        }
      },
      "userId" : "W6pK2HHA1TZC9wicnCaODQhHvoi1"
    },
    "lgmSZ1HnMnSzE71kCLfdxK8AN2G2" : {
      "age" : 18,
      "email" : "weirhe@gmail.com",
      "firstname" : "james",
      "lastname" : "leen",
      "latitude" : 37.3325232,
      "longitude" : -122.0286527,
      "profilePic" : "users/profilePhoto/W6pK2HHA1TZC9wicnCaODQhHvoi1.jpg",
      "receivePostRequest" : {
        "status" : true,
        "tag" : "tag 1"
      },
      "userId" : "lgmSZ1HnMnSzE71kCLfdxK8AN2G2"
    }
  }
}
SwiftER
  • 1,235
  • 4
  • 17
  • 40

2 Answers2

0

try..

print(snapshot.childSnapshotForPath("firstname").value)
Ymmanuel
  • 2,523
  • 13
  • 12
0

Careful with the 1 and true as they will return different results

If you have an integer value of 1 stored in Firebase

node_0
  test: 1
node_1
  test: true
node_2
  test: 1

if the query is

queryOrderedByChild("test").queryEqualToValue(true)

only node_1 will be returned, as it's true

Likewise

queryOrderedByChild("test").queryEqualToValue(1)

only node_0 and node_2 will be returned as they are 1

And to answer the question:

print(snapshot.value) //prints all of the data in the node

and when you are returning .Value, the snapshot will have children so one way to access the keys of each child is:

for child in snapshot.children {
  let key = child.key as String
  print(key)
}

this will print each parent node key, lgmSZ1HnMnSzE71kCLfdxK8AN2G2 in this case

Edit

In response to the comment/question

but im also trying retrieve "tag" : "tag 1

There is not an initially obvious way to get to that from a child = snapshot.children loop, so here's how you do it

for child in snapshot.children {

     let receivePostRef = child.childSnapshotForPath("receivePostRequest")
     let aTag = receivePostRef.value["tag"]
     print("setting = \(aTag)") //print's Tag1
}
Jay
  • 34,438
  • 18
  • 52
  • 81
  • your suggestion did work for the first part, but im also trying retrieve ` "tag" : "tag 1` i tried this `for child in snapshot.children { print(child.value["receivePostRequest/status"]) let key = child.key as String print(key) }` – SwiftER Jun 05 '16 at 00:00
  • @peter That's a super great question and it took me a while to think through it. I've updated my answer with the additional answer. I would suggest reviewing your data structure as your receivePostRequest node is inconsistent. The format is different from one user to another and thats going to cause you grief; one has a 'tags' node and one does not. Either make the all have a tags: node with children or have them all just have a single tag child. – Jay Jun 05 '16 at 13:04
  • that work great your answer have been accepted. I will be updating the database also – SwiftER Jun 06 '16 at 13:35