【问题标题】:Filter products based on the sub child in Firebase根据 Firebase 中的子子项过滤产品
【发布时间】:2015-09-11 15:25:24
【问题描述】:

我试图弄清楚如何根据 Firebase 中的子子节点过滤产品。

我的设置如下:

products/
     product1
        /author: 12345
        /title: "Awesome"
        /description: "more awesome"
     product2
        /author: 67890
        /title: "Awesome"
        /description: "more awesome"

如何查询 Firebase 以便仅检索它所持有的 child($productId).child(author) 等于 12345 的产品?

我尝试了以下方法,但这显然不起作用。我需要一种方法来过滤子子项:

var ref = new Firebase(FBURL);

// Attach an asynchronous callback to read the data at our posts reference
ref.child("products").child('$productId').child('author').equalTo(12345).on("value", function(snapshot) {
      console.log(snapshot.val());
    }, function (errorObject) {
      console.log("The read failed: " + errorObject.code);
});

【问题讨论】:

  • 目前在 Firebase 中无法按较低级别的子项进行排序/过滤。见stackoverflow.com/questions/27207059/…
  • 我最初误解了你的问题。可以过滤子节点的属性。在下面回答。

标签: javascript angularjs firebase nosql


【解决方案1】:

只需使用orderByChild():

ref.child("products").orderByChild('author').equalTo(12345)...

这将通过author 属性对products 下的每个子节点进行排序,然后仅返回值为12345 的子节点。

Firebase's documentation on queries

【讨论】:

  • 嗯认为您的第一条评论实际上是正确的。设置是: products/$productId/author 其中 $productId 是动态的。产品的子代不是作者还是那不重要??
  • 可以对子属性的值进行排序。我提供的代码 sn-p 将完成此操作。
  • 对不起!极好的!有用! Firebase 的真正好功能。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-17
  • 2018-09-08
  • 2019-05-11
相关资源
最近更新 更多