Firestore更新数据未捕获(承诺)FirebaseError:缺少权限或权限不足

时间:2020-04-10 01:26:29

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

我正在尝试更新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个小时试图弄清楚为什么我得到错误消息,但没有运气。请为我指出正确的方向。

1 个答案:

答案 0 :(得分:0)

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