首页 > 解决方案 > 更改规则时,Firebase 数据库权限被拒绝

问题描述

更新这样的规则后,我无法在反应本机应用程序中访问 firebase 数据库

{
  "rules": {
     "main_data":{
         "$uid": {
           "xx1":{
             ".read": "auth != null && auth.uid == $uid",
             ".write": "auth != null && auth.uid == $uid",
           },
             "xx2":{
             ".read": "auth != null && auth.uid == $uid",
             ".write": "auth != null && auth.uid == $uid",
             }
        },
    },
  }
}

我有像这样的json结构

main_data:{
    {"uid":{
       "xx1":{"id":"uid","d2":"val2","d3":"val3","d4":"d4","d5":"val5","d6":"val6"}
      },
       "xx2":"value"}
   }

我在反应本机应用程序中遇到了这个错误

@firebase/database: FIREBASE WARNING: update at /main_data/UID failed: permission_denied

我通过以下代码访问它

firebase.database().ref('main_data').child(UID).child("xx1 or xx2")
            .on("value", data=>{
                console.log(data)
            })

更新 :我忘了分享写代码

 firebase.database().ref('main_data/' + UID).update({
    xx2: "Some_value",
 }).then(() => {
    console.log("Success!!")
 }).catch((e) => {                                                                        
    console.log(e)
 })

标签: javascriptfirebasefirebase-realtime-databasefirebase-security

解决方案


我在浪费时间后解决了它,实际上是我在项目中使用的包不匹配问题


推荐阅读