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
- 当木偶师打开Chrome时,不能使用Chrome扩展 2022-01-01
- 在 Javascript 中使用 Fetch API 上传文件并显示进度 2022-01-01
- 使用 jQuery 在悬停时交换 DIV 类 2022-01-01
- 逻辑运算符 ||在 javascript 中,0 代表 Boolean false? 2022-01-01
- CORS:当凭据标志为真时,无法在 Access-Control-Allow-Origin 中使用通配符 2022-01-01
- HTML5 画布调整为父级 2022-01-01
- 带角度的选项卡:仅使用 $http 在单击时加载选项卡 2022-01-01
- 最佳动态 JavaScript/JQuery 网格 2022-01-01
- 从快速中间件中排除路由 2022-01-01
