我在firestore集合中有文件称为'库存',具有以下结构:
{
creator: string,
images: string[],
}
creator字段是创建文档的用户的uid。
在我的firestore规则部分,我有以下规则:
service cloud.firestore {
match /databases/{database}/documents {
match /inventories/{inventoryid} {
allow read, write: if request.auth.id == resource.data.creator;
}
}
}
在我的离子应用中,我做了以下几点:
this.inventoryCollection = database.collection<Inventory[]>
('inventories', ref => ref.where('creator', '==', auth.currentUserId));
执行时出现以下错误:
Missing or insufficient permissions.
谁能看到我做错了什么?这是我的规则的问题吗?或者我的代码调用firestore是一个问题?
您应该使用request.auth.uid
而不是request.auth.id
,请参阅https://firebase.google.com/docs/reference/rules/rules.firestore.Request#auth
此外,您应该将规则划分为两个“子规则”,如下所示:
service cloud.firestore {
match /databases/{database}/documents {
match /inventories/{inventoryid} {
allow read: if request.auth.id == resource.data.creator;
allow write: if request.auth.id == request.resource.data.creator;
}
}
}
请注意使用request.resource
作为write
规则。
在关于Firestore安全规则的官方以下视频中对此进行了很好的解释:https://www.youtube.com/watch?v=eW5MdE3ZcAw