获得单品火力棒

时间:2016-06-15 12:16:48

标签: firebase angularfire firebase-realtime-database

我从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查询都是错误的。愿意开悟。

2 个答案:

答案 0 :(得分:2)

我猜您的数据结构如下:

System ID: .../billel.xml
Main validation file: .../billel.xml
Schema: .../billel.xsd
Engine name: Xerces
Severity: error
Description: cvc-complex-type.2.4.a: Invalid content was found 
  starting with element 'b'. One of '{a}' is expected.
Start location: 4:3
End location: 4:4
URL: http://www.w3.org/TR/xmlschema-1/#cvc-complex-type

你正在查询" codeId"。

我建议在" codeId"下添加codeId(键/值)。要查询的对象,如下所示:

System ID: .../billel.xml
Main validation file: .../billel.xml
Schema: .../billel.xsd
Engine name: Saxon-EE 9.6.0.7
Severity: error
Description: In content of element <container>: The content model 
  does not allow element <b> to appear immediately after element <a>. 
  Expected <a> or nothing. 
Start location: 4:18

现在你可以这样做:

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

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

答案 1 :(得分:1)

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

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