首页 > 解决方案 > 错误:使用 forEach 时没有为 id 定义索引

问题描述

我想按顺序使用 Firebase 获取我的数据列表。使用该orderByChild功能时,forEach我收到此错误:

Error: No index defined for id

这是数据:

<UID1>
-----some data...
<UID2>
-----some data...
dinos
---bronto
   |--- id: 100
---stega
   |--- id: 200
---tyrano
   |--- id: 110

这是我的规则集:

{
  "rules": {
    "dinos": {
      ".indexOn": "id"
    },
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

这是获取数据的代码:

    async function orderDinos(){
        let orderedDinos = await db.ref('dinos').orderByChild('id')

        let get = await orderedDinos.get()
        // console.log(get.val())
        get.forEach((myDino) => {
            console.log(myDino.val())
        })
    }

我做错了什么?为什么我的代码不起作用?

标签: javascriptfirebasefirebase-realtime-database

解决方案


firebaser here

There is an issue with get() and indexes in some cases. We're working on a fix, but in the meantime you can work around the problem by calling once() instead of get().

Also see: Realtime database - orderByChild() works with once(), doesn't work with get()


推荐阅读