0

我想将一些数据保存在firebase中的Items节点上。我能救我的时候规则是Firebase DatabaseError:权限被拒绝

"Items":{ 
    ".write": "auth != null", 
    ".read": "auth != null" 
} 

,但我想用户编写/更新他们自己项目和我不想使用$变量,因为它会增加我的道路上的一些数据。这是

下面的方法将增加我的路径代替/Items上的某些数据将是/key/Items

{ 


"rules": { 
    "Items": { 
     "$user_id": { 
     ".read": "auth != null", 
     ".write": "$user_id === auth.uid" 
     } 
    } 
    } 
} 

因此,我看到的最好的方法就是写我的规则是这样

"Items":{ 
     ".read": "auth != null", 
     ".write": "(data.exists() && data.child('userId').val() === auth.uid) || (!data.exists() && newData.child('userId').val() === auth.uid)" 

} 

} 

当我在模拟器上运行以下代码时,一切正常

{ 
"userId":"xxxxxxxxxxxxxxxxx" 
} 

wnen我试图从我的手机上传数据,我得到一个错误

setValue at /Items/-KWH6tgdLPGTdascf-w3 failed: DatabaseError: Permission denied 

我要去哪里错了?

回答

0

组数据库的读写规则true

"Items":{ 
    ".write": "true", 
    ".read": "true" 
} 
+1

,但将让大家编辑数据。我只想要数据所有者这样做 –

+0

在这种情况下,您只需使用'$',检查[this](http://stackoverflow.com/questions/31736252/firebase-user-read-all-write-new - 但只修改他自己的数据)和[这](https://stackoverflow.com/questions/38600989/keeping-firebase-data-secure) –

+0

添加“$”将迫使我包括一些额外的数据在我的道路上。那就是我的数据库引用应该指向'/ items/key /'。我不想包含任何密钥 –