queryOrderBy中的Firebase访问键 [英] Firebase access keys in queryOrderBy

查看:116
本文介绍了queryOrderBy中的Firebase访问键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个与此查询相关的问题:


  1. 我在Firebase中执行查询,查询返回结果我正在寻找,但当我试图访问查询中的数据,它是零


  2. 如何访问查询中的标签密钥?这是1深


    预先感谢您

    Query

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

    JSON结果

     可选({
    lgmSZ1HnMnSzE71kCLfdxK8AN2G2 = {
    age = 18;
    email =
    firstname = Jamie;
    lastname = lon;
    latitude =37.3325232;
    longitude =-122.0286527;
    profilePic = users / profilePhoto / W6pK2HHA1TZC9wicnCaODQhHvoi1.jpg;
    receivePostRequest = {
    status = 1; $ b $ tag = tagSample;
    $ b $;}
    userId = lgmSZ1HnMnSzE71kCLfdxK8AN2G2 ;
    };
    })

    这是完整的查询

      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:日期和时间,
    postDescription:详细信息,
    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,
    longitudinal:-122.03014382,
    status:false,
    tags:{
    tag1:444
    }
    },
    userId:W6pK2HHA1TZC9wicnCaODQhHvoi1

    $ blgmSZ1HnMnSzE71kCLfdxK8AN2G2:{
    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
    }
    }
    }



    如果Firebase中存储的整数值为1

      node_0 
    测试:1
    node_1
    测试:true
    node_2
    test:1

    如果查询是

      queryOrderedByChild(test)。queryEqualToValue(true)



    <只有node_1会被返回,因为它是真的。同样,

    pre $ > queryOrderedByChild(test)。queryEqualToValue(1)

    只有node_0和node_2会返回是$ 1

    并回答这个问题:

    $ p $ print(snapshot .value)//打印节点中的所有数据

    当您返回.Value时,快照wi所以有一个方法来访问每个孩子的钥匙是:

     在snapshot.children中的孩子{
    让key = child.key作为字符串
    print(key)
    }

    这个会打印每个父节点的键值,lgmSZ1HnMnSzE71kCLfdxK8AN2G2在这种情况下



    为了回应评论/问题


    但是我也试着检索tag:tag 1


    从child = snapshot.children循环中找不到最初明显的方法,所以以下是如何操作的方法

     为snapshot.children中的子元素{

    let receivePostRef = child.childSnapshotForPath(receivePostRequest)
    让aTag = receivePostRef.value [tag]
    print(setting = \(aTag))// print's Tag1
    }


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

    解决方案

    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
    }
    

    这篇关于queryOrderBy中的Firebase访问键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆