【问题标题】:Is it possible to only retrieve a list of keys from Firebase?是否可以仅从 Firebase 检索密钥列表?
【发布时间】:2015-12-10 11:00:19
【问题描述】:

我有一个这种形式的节点:

index
   $categoryId
     $productId

我希望主要检索index 的键列表,因此一个对象填充了索引中所有可用的$categoryId。

有没有办法在 Firebase 中检索此列表?我假设检索键列表比使用来自$categoryId 的所有数据的列表更有效。

我知道另一种方法是维护所有类别的索引列表,但这需要更多编码(当类别被编辑和删除时)。

【问题讨论】:

    标签: javascript firebase nosql


    【解决方案1】:

    您可以在 REST API 中使用 shallow keys option,但首选索引。

    Plnker demo.

    fetch('<my-firebase-app>/category/.json?shallow=true')
      .then((response) => {
        return response.json()
      })
      .then((data) => console.log(data)) // prints only keys under category
    

    很遗憾,实时 SDK 中没有解决方案。但是,我建议您对数据进行结构化以避免使用 REST API。尝试在 Firebase 数据库中存储 $categoryId 的索引。我知道这是额外的代码,但通常是单行代码。

    {
      "lookupCategory": {
        "category_one": true,
        "category_two": true,
        "category_three": true,
        "category_four": true,
        "category_five": true
      }
    }
    

    使用此索引,您可以进行实时收听,如果愿意,也可以使用.once()。

    var ref = new Firebase('<my-firebase-app>/lookupCategory');
    ref.on('value', (snap) => console.log(data));
    

    【讨论】:

    • 有道理。这是您在 Firebase 的工作,还是不相关?
    • 它是相关的,但我现在没有更新。同时希望这对你有用。
    • @DavidEast 请回答我关于 Firebase 的问题,这里是链接 stackoverflow.com/questions/42646255/… 提前谢谢
    猜你喜欢
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 2017-02-12
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    相关资源
    最近更新 更多