2017-10-08 43 views
6

尝试写入Firestore时出现错误。在访问规则中使用字段写入Firestore时缺少权限或权限不足

我正在尝试为我的安全规则使用包含用户uid的字段。

service cloud.firestore { 
    match /databases/{database}/documents { 

    match /messages/{document=**} { 
     allow read: if resource.data.user_uid == request.auth.uid; 
     allow write: if resource.data.user_uid == request.auth.uid; 
    } 
    } 
} 

如果我在公司的FireStore数据库具有数据,所读取的规则工作正常 - 但是当我试图写我得到:Error: Missing or insufficient permissions. 有什么是我做错了这个写入规则?

P.S.如果我更改了规则,这一点,我能写我的数据库 - 但是这不是我的目的,足够安全:

match /messages/{document=**} { 
     allow read: if resource.data.user_uid == request.auth.uid; 
     allow write: if request.auth != null; 
    } 

回答

7

resource.data是指已存储的数据,让您的规则允许用户只更新已包含其用户标识的数据。

什么你可能要检查是request.resource.data这是在新的数据来

有关于这些领域和其他安全规则在这里相当广泛的文件:https://firebase.google.com/docs/firestore/security/secure-data

+0

感谢那些工作。 Firebase文档需要更新,他们只是举例阅读,他们也应该举例写作...... –

+0

@LeoFarmer这里有一个关于不同安全规则的相当广泛的文档:https://firebase.google.com/文档/公司的FireStore /安全性/安全数据 – Scarygami