Tucker Powers
Tucker Powers

Reputation: 31

Firestore update data Uncaught (in promise) FirebaseError: Missing or insufficient permissions

I am trying to update pre-existing information from the cloud firestore. Here is my rules:

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;
    }
  }
}

so I can read the data by userId and it reads fine on my website, but when I try to update it, I get the following error message

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

Here is my code to update the account info

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();
        }
    })
});

I have spent 5 hours trying to figure out why I get the error message but had no luck. Please point me to the right direction.

Upvotes: 3

Views: 3141

Answers (2)

Anshuman Bisoyi
Anshuman Bisoyi

Reputation: 236

You did a minor spelling mistake like I had done, always write correct spellings of the db collections. In your case it should be "users" instead of "user" in the code. db.collection('users').doc(user.uid).set

Upvotes: 2

Doug Stevenson
Doug Stevenson

Reputation: 317362

Your code is using the collection name "user", but your rules and database are using "users" plural. They need to match exactly.

Upvotes: 6

Related Questions