0

I have a data saved like this:

users : {
   '1' : {
      'name':'jack',
      'old':21,
      'paymentCard':{
        ....
      }
   }
}

When I fetch my user like this:

ref.child('users/1').once('value', snapshot => {
   console.log(snapshot.val()); 
});

The result contain the paymentCard.

Is there a way to restrict child returned ?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Charly berthet
  • 1,178
  • 5
  • 15
  • 31

1 Answers1

3

That's not possible with your current data structure. As mentioned in Firebase docs: Best Practices for Data Structure @ https://firebase.google.com/docs/database/ios/structure-data#best_practices_for_data_structure

when you fetch data at a location in your database, you also retrieve all of its child nodes. In addition, when you grant someone read or write access at a node in your database, you also grant them access to all data under that node. Therefore, in practice, it's best to keep your data structure as flat as possible.

So you should ideally have your data structure like:

users : {
   '1' : {
      'name':'jack',
      'old':21,
   }
}
paymentCard: {
   '1' : { // user id
        ....
   }
}

That way when you fetch data at users/1 you will only get name and old values and to fetch payment card details you will need to access paymentCard/1.

Vivek Athalye
  • 2,974
  • 2
  • 23
  • 32