【问题标题】:Get single item firebase获取单项火力基地
【发布时间】:2016-10-16 13:21:26
【问题描述】:

我从 Firebase 数据库中的查询开始,并且会喜欢更多的见解。

此查询适用于从 firebase 数据库中检索单个项目。我的理解内联。

  var ref = DatabaseRef; // root of my fbase db
  var codeRef = ref.child('codes'); // at root/codes/ now

  codeRef
    .child($stateParams.codeId) // a param comes in
    // grab value one-time 
    // https://www.firebase.com/docs/web/guide/retrieving-data.html#section-reading-once
    .once('value', function(snap) {
      console.log(snap.val());
    })

以上工作。但是下面没有,为什么?我的理解内联。

// same ref from above
codeRef
  // traverse the /codes/ url, and find all items that match
  // the specified param
  .equalTo($stateParams.codeId)
    .once('value', function(snap) {
      console.log(snap.val()); // returns `null`
    })

相反,我希望显示与该 ID 匹配的所有项目。在这种情况下,id 是唯一的,因此,我希望得到一个单品。但是,会返回 null

来自docs

equalTo() 方法允许我们根据精确匹配进行过滤。与其他范围查询一样,它将针对每个匹配的子节点触发。

所以,也许我看错了整个 Firebase 查询。会喜欢开悟。

【问题讨论】:

    标签: firebase angularfire firebase-realtime-database


    【解决方案1】:

    equalTo() 是一种过滤方法。您需要同时使用订购方法。查看latest docs。所以可能是这样的:

      codeRef.orderByKey().
      .equalTo($stateParams.codeId)
        .once('value', function(snap) {
          console.log(snap.val()); // returns `null`
        })
    

    【讨论】:

      【解决方案2】:

      我猜你的数据结构如下:

      "codes" : {
              "codeId": {
                  ....: ....
                  }
              }
      

      您正在查询“codeId”。

      我建议在“codeId”对象下添加codeId(key/value)进行查询,如下:

      "codes" : {
              "-yabba_dabba_doo": { // codeId 
                  codeId : "-yabba_dabba_doo"
                  }
              }
      

      现在你可以这样做了:

      ref.child('codes').orderByChild('codeId').equalTo('-yabba_dabba_doo')
      

      如果我没有正确猜出您的数据层次结构,请分享您的 Firebase 数据层次结构以帮助您。

      【讨论】:

        猜你喜欢
        • 2016-09-17
        • 1970-01-01
        • 1970-01-01
        • 2014-07-14
        • 2016-12-11
        • 2017-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多