简体   繁体   English

快照侦听器中的未捕获错误:,FirebaseError:[code = permission-denied]:缺少或权限不足

[英]Uncaught Error in snapshot listener:, FirebaseError: [code=permission-denied]: Missing or insufficient permissions

I am trying to let the user only read and write their own data.我试图让用户只读写他们自己的数据。 My rules are as follow(from the docs)我的规则如下(来自文档)

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId} {
      allow read, update, delete: if request.auth != null && request.auth.uid == userId;
      allow create: if request.auth != null;
    }
  }
}

The uid for my user matches my document id but i still get the error:我的用户的 uid 与我的文档 ID 匹配,但我仍然收到错误消息:

Uncaught Error in snapshot listener:快照侦听器中未捕获的错误:
FirebaseError: [code=permission-denied]: Missing or insufficient permissions. FirebaseError:[code=permission-denied]:权限缺失或不足。

My code for getting uid to document id我获取 uid 到文档 id 的代码

const handleSignUp = async () => {
      auth
      .createUserWithEmailAndPassword(email, password)
      .then(async (UserCredentials) => {
        const user = UserCredentials.user;
        console.log("Registered with: ", user.email);

        try {
          const uidRef = doc(db, 'users', user.uid);

          const docRef = await setDoc(uidRef, {
            name: name,
            age: age,
            currentWeight: currentWeight,
            goalWeight: goalWeight,
          });

        } catch (e) {
          console.error("Error adding document: ", e);
        }

I am really lost as I have tried many different ways and all docs / answers on here do not work for me.我真的迷路了,因为我尝试了很多不同的方法,这里的所有文档/答案都不适合我。 I am guessing the error comes when i call snapshot in this code我猜当我在此代码中调用快照时会出现错误

const getUser = async() => {

    const subscriber = onSnapshot(usersRef, (snapshot) => {
      let user = []
        snapshot.docs.forEach((doc) => {
          user.push({...doc.data(), key: doc.id })
        })
        setUser(user);
        console.log(user);
    })
    return () => subscriber();
  };

I am just unsure as to what is exactly wrong here.我只是不确定这里到底出了什么问题。 Is it my rules?这是我的规则吗? My snapshot?我的快照?

Given that you get a QuerySnapshot result, I suspect that your code is reading the entire users collection.鉴于您获得了QuerySnapshot结果,我怀疑您的代码正在读取整个users集合。 But as the documentation says rules are not filters , but instead merely ensure that your code only tries to access data that it is permitted to.但正如文档所说, 规则不是过滤器,而只是确保您的代码仅尝试访问允许的数据。

So your code should only try to read the document of the currently signed in user.所以你的代码应该只尝试读取当前登录用户的文档。

const getUser = async() => {
  if (getAuth().currentUser) {
    const uidRef = doc(db, 'users', getAuth().currentUser.uid);

    const subscriber = onSnapshot(uidRef, (doc) => {
      setUser({...doc.data(), key: doc.id })
    })

    ...
  }
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 firestore:PERMISSION_DENIED:权限缺失或不足 - firestore: PERMISSION_DENIED: Missing or insufficient permissions “权限被拒绝:权限缺失或不足。” - 'Permission denied: Missing or insufficient permissions.' Firebase Cloud Functions Firestore 触发器产生:错误:7 PERMISSION_DENIED:权限缺失或不足 - Firebase Cloud Functions Firestore Trigger produces: Error: 7 PERMISSION_DENIED: Missing or insufficient permissions 在 GDI CLI 中设置新管理员时权限被拒绝 - permission-denied while setting a new Admin in GDI CLI ERROR 错误:未捕获(承诺中):错误:权限被拒绝 Firebase - ERROR Error: Uncaught (in promise): Error: Permission denied Firebase cloud_firestore/permission-denied 调用者无权执行指定操作 - cloud_firestore/permission-denied The caller does not have permission to execute the specified operation Firestore 数据库规则缺失或权限不足 - Missing or insufficient permissions firestore database rules 注销后出现缺少或权限不足的错误 - Missing or insufficient permissions errors after logout 无法从项目 B 中的云 function 访问项目 A 中的云 Firestore - “403 权限缺失或不足” - unable to access the cloud Firestore in Project A from cloud function in Project B - '403 Missing or insufficient permissions' Firebasse 中的错误“assert.ts:128 Uncaught FirebaseError: Firebase: Error (auth/invalid-api-key) - Error in Firebasse"assert.ts:128 Uncaught FirebaseError: Firebase: Error (auth/invalid-api-key)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM