Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase Firestore数据库规则:使用其他文档中的数组_Firebase_Google Cloud Firestore_Firebase Security - Fatal编程技术网

Firebase Firestore数据库规则:使用其他文档中的数组

Firebase Firestore数据库规则:使用其他文档中的数组,firebase,google-cloud-firestore,firebase-security,Firebase,Google Cloud Firestore,Firebase Security,我正在尝试制作一个web应用程序,根据我授予用户的权限显示不同的元素 所有权限都存储在云Firestore数据库的/users/{userId}字段“permissions”中,该字段是一个包含permissionId的数组 在/photo_libraries/{libraryId}中,我有一个名为permissionId的字段,它是一个字符串 现在,我想让具有正确权限ID的用户能够阅读/photo_libraries/{libraryId}中具有该权限ID的文档 我试过这个: rules_ve

我正在尝试制作一个web应用程序,根据我授予用户的权限显示不同的元素

所有权限都存储在云Firestore数据库的/users/{userId}字段“permissions”中,该字段是一个包含permissionId的数组

在/photo_libraries/{libraryId}中,我有一个名为permissionId的字段,它是一个字符串

现在,我想让具有正确权限ID的用户能够阅读/photo_libraries/{libraryId}中具有该权限ID的文档

我试过这个:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {    
    
    match /users/{userId} {
        allow read: if request.auth.uid == userId;
    }
    
    match /photo_libraries/{libraryId} {
        allow read: if get(/database/$(database)/documents/photo_libraries/$(libraryId)).data.permissionId in get(/databases/$(database)/documents/users/$(request.auth.uid)).data.permissions;
    }
  }
}
但这似乎不起作用,我对消防商店的规定还很陌生。有人能帮我吗

另外,我的数据库是这样的:

这是我尝试运行的代码:

const db = firebase.firestore(); const auth = firebase.auth();
auth.onAuthStateChanged(user => {
    if (user) {
        db.collection('photo_libraries').get().then(snapshot => {
            // set up the UI
        }, err => {
            console.log(err.message);
        });
    } else {
        // Logging out stuff
    };
});
在控制台中,我收到错误消息:

权限缺失或不足。

谢谢,

乔纳斯

试试这个:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {    
    
    match /users/{userId} {
        allow read: if request.auth.uid == userId;
    }
    
    match /photo_libraries/{libraryId} {
        allow read: if resource.data.permissionId in get(/databases/$(database)/documents/users/$(request.auth.uid)).data.permissions;
    }
  }
}
但更好的解决方案是将
permissions
数组添加为自定义声明,这样就不需要调用
get


查询Firestore时,请确保您只查询您可以实际访问的文档。在谷歌上搜索“规则不是过滤器”,你会在SO和Firebase官方文档中获得大量点击。

当我运行
const db=Firebase.firestore()时;const auth=firebase.auth();auth.onAuthStateChanged(user=>{if(user){db.collection('photo_libraries').get()。然后(snapshot=>{//setup the UI},err=>{console.log(err.message);});}否则{//Logging out stuff};})它会在控制台中给我一条错误消息,说“缺少或权限不足”,即使是您建议的答案。另外,我已经在数据库的帖子中添加了一些图片。你需要确保你只是在查询你真正可以访问的文档。在谷歌上搜索“规则不是过滤器”,你会在SO和Firebase官方文档中获得大量点击。非常感谢你的提示!我找到了答案,我使用了:
db.collection('photo-libraries')。其中(“permissionId”,“in”,userPermissions)