code đờ
code đờ

Reputation: 615

How to get all child keys in Firebase?

I have a Firebase (Real-time Database) node with a large number of children. I want to list all child keys, so I used this code:

reference.once('value', snapshot=>{
    snapshot.forEach(child => {
        console.log(child.key);
    })
})

then Firebase will throw an exception:

The specified payload is too large, please request a location with less data.

So I don't know how to get the child keys (only keys) without getting that error. Please help!

Upvotes: 1

Views: 1239

Answers (2)

ntson9p
ntson9p

Reputation: 102

Use Firebase's shallow query https://firebase.google.com/docs/database/rest/retrieve-data#shallow

For example, if your data is structured like this

parent: {
  child_0: {...<large data>...},
  child_1: {...<large data>...},
  child_2: "abc",
  child_3: 123
}

The shallow query's result will be:

parent: {
  child_0: true,
  child_1: true,
  child_2: "abc",
  child_3: 123
}

Upvotes: 3

Đ&#224;m T&#249;ng
Đ&#224;m T&#249;ng

Reputation: 369

Some unexpected errors can send a cancel event and terminate the connection. The cause is described in the data provided for this event. Some potential causes are as follows: 1. The Firebase Realtime Database Rules no longer allow a read at the requested location. The data description for this cause is "Permission denied." 2. A write triggered an event streamer that sent a large JSON tree that exceeds our limit, 512MB. The data for this cause is "The specified payload is too large, please request a location with less data."

Since realtime database only limit 500Mb per request so in order to execute this request you should try to paging a query with or try to iterate over children using child_added listener instead of querying all of the childs.

Upvotes: 1

Related Questions