【问题标题】:Firestore update data Uncaught (in promise) FirebaseError: Missing or insufficient permissionsFirestore 更新数据未捕获(承诺中) FirebaseError:缺少权限或权限不足
【发布时间】:2020-07-22 18:08:00
【问题描述】:

我正在尝试从 Cloud Firestore 更新预先存在的信息。这是我的规则:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {

        //match logged in user doc in users collection
    match /users/{userId} {
      allow read, write, update: if request.auth.uid == userId;
      allow create: if request.auth.uid != null;
    }
  }
}

所以我可以通过 userId 读取数据,并且它在我的网站上读取良好,但是当我尝试更新它时,我收到以下错误消息

error.ts:166 Uncaught (in promise) FirebaseError: Missing or insufficient permissions.
    at new hi (https://www.gstatic.com/firebasejs/7.9.3/firebase-firestore.js:1:51421)
    at https://www.gstatic.com/firebasejs/7.9.3/firebase-firestore.js:1:316738
    at br.<anonymous> (https://www.gstatic.com/firebasejs/7.9.3/firebase-firestore.js:1:315592)
    at Jt (https://www.gstatic.com/firebasejs/7.9.3/firebase-firestore.js:1:15221)
    at br.I.dispatchEvent (https://www.gstatic.com/firebasejs/7.9.3/firebase-firestore.js:1:16063)
    at Nr.ua (https://www.gstatic.com/firebasejs/7.9.3/firebase-firestore.js:1:45312)
    at nr.I.Fa (https://www.gstatic.com/firebasejs/7.9.3/firebase-firestore.js:1:43219)
    at ze (https://www.gstatic.com/firebasejs/7.9.3/firebase-firestore.js:1:21453)
    at qe (https://www.gstatic.com/firebasejs/7.9.3/firebase-firestore.js:1:20854)
    at xe.I.Ja (https://www.gstatic.com/firebasejs/7.9.3/firebase-firestore.js:1:23264)

cloud firestore image

这是我更新帐户信息的代码

const updateForm = document.querySelector('#update-form');
updateForm.addEventListener('submit', (e) => {
    e.preventDefault();

    auth.onAuthStateChanged(user => {
        console.log(user.uid);
        if(user) {
            db.collection('user').doc(user.uid).set({

                // email: updateForm['update-email'].value,
                name: updateForm['update-name'].value,
                // dob: updateForm['update-dob'].value,
                // phone: updateForm['update-phone'].value

            })
        } else {
            updateAccountInfo();
        }
    })
});

我花了 5 个小时试图弄清楚为什么我收到错误消息但没有运气。请指出正确的方向。

【问题讨论】:

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


    【解决方案1】:

    您的代码使用集合名称“user”,但您的规则和数据库使用“users”复数形式。它们需要完全匹配。

    【讨论】:

    • 如果您有时间,Stack Overflow 上习惯使用左侧的复选标记按钮将有用的答案标记为正确,这样回答问题的人就会获得荣誉。
    猜你喜欢
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-24
    • 2021-04-22
    • 2022-01-25
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多