【问题标题】:Error in adding indexing to json file for fetching data from firebase.Invalid JSON. Keys cannot be empty or contain $ # [ ] / or将索引添加到 json 文件以从 firebase 获取数据时出错。无效的 JSON。键不能为空或包含 $ # [ ] / 或
【发布时间】:2019-07-17 04:02:16
【问题描述】:

我正在尝试根据某些条件(例如分数是否大于 50)使用邮递员从 firebase 获取数据。为此,我应该根据文档链接在我的 JSON 文件中添加索引: https://firebase.google.com/docs/database/security/indexing-data.html

所以我根据文档编写的代码是:


{
    "rules": {
        "scores": {
            ".indexOn": ".value"
        }
    },
    "scores": {
        "bruhathkayosaurus" : 55,
        "lambeosaurus" : 21,
        "linhenykus" : 80,
        "pterodactyl" : 93,
        "stegosaurus" : 5,
        "triceratops" : 22
    }
}

但是当我将此 json 上传到 firebase 时,我收到了错误:

无效的 JSON。键不能为空或包含 $# [ ] / 或 。

我不知道我写错了什么。规则应该写在其他地方还是其他格式?我是初学者,所以知识不多。

【问题讨论】:

    标签: json firebase firebase-realtime-database postman


    【解决方案1】:

    规则应该只写在 firebase 中,而不是我们上传的 json 文件中。规则将根据文档和您的要求(您想要获取的内容)编写,但在图像中显示的区域(红色圆圈中):

    Select Database -> Realtime Database -> Rules

    在屏幕截图中,我包含了根据分数值获取数据的规则。

    【讨论】:

      【解决方案2】:

      根据 firabase 文档,您必须使用他们的控制台或 CLI(从 NPM 安装)来编辑索引。

      This link 提供更多详细信息。

      【讨论】:

      • 不,它也不起作用。邮递员生成错误:“索引未定义,添加 \".indexOn\": \".value\", for path \"/address\", to the rules"。我认为它正在按照文档中的建议在“规则”中寻找 .indexOn ,但它在通过 CLI 自动生成的文件中不可用。
      • 您不应该使用邮递员进行索引编辑,文档专门解释了如何使用 CLI(是的,您需要编辑生成的 JSON 文件并添加您需要的索引):firebase.google.com/docs/firestore/query-data/…
      • 我使用 CLI 生成索引并进行了编辑,然后再次上传。我只是想使用产生上述错误的 POSTMAN 发出 GET 请求。
      猜你喜欢
      • 2016-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-12
      • 1970-01-01
      • 2021-11-21
      • 2011-06-20
      • 2020-08-04
      相关资源
      最近更新 更多