Firestore no permissions to execute operation(FiRestore没有执行操作的权限)
本文介绍了FiRestore没有执行操作的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在FiRestore中建立规则,其中每个人都可以读取彼此的内容,但只有文档的所有者可以创建、写入、更新或删除这些内容。
我在FiRestore中设置了以下规则:
rules_version = '2';
service cloud.firestore {
match /databases/{database} {
match /codes/{userID} {
allow create, write, update, delete: if request.auth.uid == userID;
allow read: if request.auth.uid != null;
}
}
}
我的FiRestore结构是:
Collection 'codes' > Document 'BNhBibYZ0ThCNCH2gzPRufFsIk22' > nothing in here
我的要求是:
firestore()
.collection('codes')
.doc(this.state.userID)
.onSnapshot((querySnapshot) => {
if (!querySnapshot) {
let testObj = {"hello": "world"}
firestore().collection('codes').doc(this.state.userID).set(testObj);
}
});
我收到的输出是:Error: [firestore/permission-denied] The caller does not have permission to execute the specified operation.
我做错了什么?
推荐答案
您可以使用我创建的以下函数来执行此操作
function isUserAuthenticated() {
return request.auth.uid != null;
}
function belongsTo(userId) {
return request.auth.uid == userId;
}
然后您可以这样使用它:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
match /codes/{userId} {
allow read : if isUserAuthenticated();
allow create : if belongsTo(userId);
allow update: if belongsTo(userId);
allow delete: if belongsTo(userId);
}
/* Functions */
function isUserAuthenticated() {
return request.auth.uid != null;
}
function belongsTo(userId) {
return request.auth.uid == userId;
}
}
}
如果对您有效,请接受作为答案。可在评论中查看更多问题。
这篇关于FiRestore没有执行操作的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:FiRestore没有执行操作的权限
基础教程推荐
猜你喜欢
- 我什么时候应该在导入时使用方括号 2022-01-01
- 动态更新多个选择框 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01