0

I have the following Firebase code:

https://gist.github.com/balazsorban44/52c2858aa10f6269c7a9b3c0cfa247d5

I would like to get a js object of all the reservations', but only the public parts, since the details are only viewable for the admins.

I tried the following:

let reservations = {} 
firebase.database().ref('reservations').once('value', snapshot => {
    snapshot.forEach(reservation => {
      reservations[reservation] = reservation.val().public
    })
})

But it does not work. Where am I failing?

Should I create a reservationsPublic and a reservationsPrivate object, so I can more easily fetch the info I need? In this case, when a user fills out a reservation form, should I split the input into two and put half of it into the reservationsPublic and the rest to the reservationsPrivate?

EDIT:

I think I found the solution for my problem here:

https://stackoverflow.com/a/38649032/5364135

So I have to write to the database twice, to improve the read performance, if I understood it correctly.

Balázs Orbán
  • 559
  • 1
  • 4
  • 26
  • Can you create a complete minimal repro here? The sample data and version info, for example, would be integral to solving the mystery. See [how to ask](http://stackoverflow.com/help/how-to-ask) and [creating an mcve](http://stackoverflow.com/help/mcve). – Kato Sep 05 '17 at 15:31
  • See also, [Security rules are not filters](https://firebase.google.com/docs/database/security/securing-data#rules_are_not_filters). – Kato Sep 05 '17 at 15:32
  • I added a gist with some sample data, and another option, which may work, but I am not sure if it is a "nice solution" – Balázs Orbán Sep 06 '17 at 07:44
  • @Kato I think, I just found the solution for my problem. But could you approve that it is right? I edited my question with a link to the solution. – Balázs Orbán Sep 06 '17 at 07:59
  • Seems like a great approach if you need to be able to iterate/query the collection. If you will always access specific records, you can probably just split them into public/ private/ child nodes and query the public/ directly. – Kato Sep 11 '17 at 22:50

0 Answers0