如何使用auth.uid变量在firebase上设置indexOn规则?

问题描述 投票:0回答:2

我试图在firebaseDatabase中的节点上设置规则,但是我收到错误

保存规则时出错 - 第85行:键名不能包含“。”,“#”,“$”,“/”,“[”或“]”(未绑定的名称以“$”开头)

据我所知,auth.uid是当前登录用户的全局变量。我怎样才能解决这个问题?

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null", 

        "notifications/auth.uid": {
        ".indexOn":["createdAt"]    
      }
  }
}
firebase firebase-realtime-database firebase-authentication firebase-security-rules
2个回答
1
投票

如果您尝试在auth.uid下为每个用户存储通知并允许通过定义索引来查询这些通知,那么您正在寻找以下规则:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null", 

    "notifications": {
      "$uid": {
        ".indexOn":["createdAt"]    
      }
    }
  }
}

这里的$uid是一个通配符,适用于notifications下的每个节点。要了解有关此内容的更多信息,请参阅Using $ Variables to Capture Path Segments


1
投票

这是问题所在

"notifications/auth.uid"

因为它将引号内的所有内容都视为字符串,因此句点导致错误,路径不能包含句点字符。此外,它不会解析auth.uid,因为它只是一个字符串,而不是你想要的变量。你可以做点什么

root.child('notifications').child(auth.uid) {...

甚至

root.child( 'notifications/' + auth.uid ) {...
© www.soinside.com 2019 - 2024. All rights reserved.