【问题标题】:Firebase Uncaught Error in onSnapshot: [FirebaseError: Missing or insufficient permissions.]onSnapshot 中的 Firebase 未捕获错误:[FirebaseError:缺少权限或权限不足。]
【发布时间】:2020-07-15 12:31:52
【问题描述】:

我的应用 (onSnapshot) 上有一个 Firestore 监听器,当我创建它时出现错误

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

useEffect(() => {
    const { firebase } = props;

    // Realtime database listene
    const unsuscribe = firebase
      .getDatabase()
      .collection("posts")
      .doc(firebase.getCurrentUser().uid)
      .collection("userPosts")
      .onSnapshot((snapshot) => {
        let changes = snapshot.docChanges();
        changes.forEach((change) => {
          if (change.type === "added") {
            console.log(change.doc.data());
            // TODO - Add the new post to the posts list
          }
        });
      });

    return () => {
      // Detach the listening agent
      unsuscribe();
    };
  }, []);

我一直在检查我的权限,但似乎很好:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    function isSignedIn() {
      return request.auth.uid != null;
    }
    
    function isSameUser(userId) {
        return request.auth.uid == userId;
    }
   
    match /posts/{userId}/userPosts {
       allow read: if true;
       allow write, update, delete: if isSignedIn() && isSameUser(userId);
    }
  }
}

如您所见,表示用户帖子的文档(在 /posts/{userId}/userPosts 中)只有在用户登录并且是同一用户时才会创建......关于什么是错了吗?

谢谢。

【问题讨论】:

    标签: javascript firebase react-native google-cloud-firestore firebase-security


    【解决方案1】:

    好的,我有解决办法。我在这里写它是因为它可能对将来的某人有用。

    只要做:

    match /posts/{userId}/userPosts/{document=**} {
       allow read: if true;
       allow write, update, delete: if isSignedIn() && isSameUser(userId);
    }
    

    并且会正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-12
      • 2021-10-31
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 2022-01-25
      • 2019-05-04
      • 2021-03-10
      相关资源
      最近更新 更多