JoeBayLD
JoeBayLD

Reputation: 999

Firestore onSnapshot rules throw error when deleting document

I'm having an issue with a Firestore observer and it's associated rules. I'm trying to have rules that only allow the user to see invites that contain the user's email address. However, if I delete a document from the collection, the snapshot observer throws an error Uncaught Error in onSnapshot: FirebaseError: Null value error. for 'get' @ L92.

Here's my rules:

 match /invites/{inviteID} {

      function isSignedIn() {
        return request.auth != null;
      }

      function isUserInvite() {
        return request.auth.token.email != null &&
        request.auth.token.email == resource.data.user_email;
      }

      allow read: if isSignedIn() && isUserInvite();
      allow write: if true;

}

And here's my listener.

firebase.firestore()
.collection("invites")
.where(`user_email`, '==', myUserEmail) // this would be the users email
.onSnapshot((snapshot) => {

  snapshot.forEach( doc => {
    console.log(doc.id, doc.data())
  })

})

It works for observing new documents, but fails when deleting a document. The following code makes the observer throw an error.

// creates a new invite and the snapshot listener informs me 
await firebase.firestore()
.collection('invites')
.doc("invite_1")
.set({
  user_email: someUserEmail, // use the users email here 
  date_added: firebase.firestore.FieldValue.serverTimestamp()
})

// deletes the invite, but the snapshot throws an error
await firebase.firestore()
.doc(`invites/invite_1`)
.delete()

UPDATE

I've found that this is only happening in the firestore emulator and not in production. Both the emulator and production have the same rules.

Upvotes: 1

Views: 760

Answers (1)

JoeBayLD
JoeBayLD

Reputation: 999

After submitting a bug report to Google - they confirmed it was a bug that only applied to the emulator. This was fixed in a new release. Here’s a link to the GitHub issue for any needed reference.

https://github.com/firebase/firebase-tools/issues/2197

Upvotes: 2

Related Questions