为什么我对 Firestore 的权限不起作用?



我的代码

created(){
let ref = db.collection('messages').orderBy('timestamp')
// subscribe to changes to the 'messages' collection
ref.onSnapshot(snapshot => {
snapshot.docChanges().forEach(change => {
if(change.type == 'added'){
let doc = change.doc
this.messages.push({
id: doc.id,
name: doc.data().name,
content: doc.data().content,
timestamp: moment(doc.data().timestamp).format('lll')
})
}
})
})

我的firebase规则:

service cloud.firestore { match /databases/{database}/documents {
match /messages/{document=**} {
allow read, update, delete: if request.auth.uid == resource.data.uid;
allow create: if request.auth.uid != null;
}
}}

我的错误

onSnapshot中的未捕获错误:FirebaseError:丢失或不足权限。

帮助解决问题

看看规则不是文档中的过滤器。

您只允许满足request.auth.uid == resource.data.uid的读取操作,但您正在尝试读取整个db.collection('messages')集合。

尝试阅读db.collection('messages').where('uid', '==', currentUserUid)

(别忘了在消息中实际放置uid属性。(


PS:你的规则看起来像是针对个人笔记,而不是用户之间的消息。如果您希望收件人与作者不同,则必须更改数据结构和规则。也许对于authorUidrecipientUid字段,我不知道,我还没有在Firestore中做过类似的事情。

最新更新