简体   繁体   English

访问 Firebase 实时数据库规则集中的 object 属性

[英]Accessing object property in Firebase Realtime Database ruleset

I store data about trips in a Firebase Realtime Database.我将有关旅行的数据存储在 Firebase 实时数据库中。 I have defined some user groups.我定义了一些用户组。 I want only users from group "vips" to read trips that have freePlaces == 0 and all the others to read the remaining trips.我只希望来自组“vips”的用户阅读具有 freePlaces == 0 的旅行,而所有其他人阅读剩余的旅行。

I tried to set such access rules:我试图设置这样的访问规则:

{
  "rules": {
      "trips":{
          "$trip":{
            ".read": "root.child('users').child('vips').child(auth.uid).exists() || $trip.child('freePlaces') > 0"
           }
       }
   }
}

but Firebase tells me $trip has no method/property 'child'.但是 Firebase 告诉我 $trip 没有方法/属性“孩子”。

I tried to do it in a different way我试着用不同的方式来做

{
  "rules": {
      "trips":{
          "$trip":{
            ".read": "root.child('users').child('vips').child(auth.uid).exists() || root.child('trips').child($trip).child('freePlaces') > 0"
           }
       }
   }
}

but Firebase says Invalid > expression: left operand must be a number or string.但是 Firebase 说 Invalid > expression: left operand must be a number or string.

What went wrong?什么地方出了错?

I recommend playing close attention to the type of each expression in your rules:我建议密切注意规则中每个表达式的类型:

  • $trip is a string , which explains why you can't call .child on it. $trip是一个string ,这解释了为什么不能在其上调用.child If you want to get a child of the node, you can just do: data.child('freePlaces') .如果你想得到节点的孩子,你可以这样做: data.child('freePlaces')
  • root.child('trips').child($trip).child('freePlaces') is a location in the database, which can't be compared with > . root.child('trips').child($trip).child('freePlaces')是数据库中的一个位置, 无法与>进行比较 If you want the value of the node, use .val() .如果您想要节点的值,请使用.val()

So combining these, your first snippet should be:所以结合这些,你的第一个片段应该是:

data.child('freePlaces').val() > 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM