Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deleting document breaks onSnapshot observer in emulator #2197

Closed
Joebayld opened this issue May 3, 2020 · 8 comments
Closed

Deleting document breaks onSnapshot observer in emulator #2197

Joebayld opened this issue May 3, 2020 · 8 comments

Comments

@Joebayld
Copy link

Joebayld commented May 3, 2020

[REQUIRED] Environment info

firebase-tools:
8.2.0

Platform:
macOS

[REQUIRED] Test case

When deleting a document that has a query on a user auth property, the following error is throw on the snapshot listener. The goal is to observe all invites that has the users email. It works properly when in production, just not in the emulator.

image

Using these 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 this basic funciton to observe the collection.

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()) // log the invites that belong to the user
  })

})

Also have a stack overflow question started: https://stackoverflow.com/questions/61567689/firestore-onsnapshot-rules-throw-error-when-deleting-document

[REQUIRED] Steps to reproduce

// 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()

[REQUIRED] Expected behavior

The snapshot should always return the query - not throw an error because an item was deleted.

[REQUIRED] Actual behavior

The error FirebaseError: Null value error. for 'get' @ L91 is thrown.

@samtstern
Copy link
Contributor

@yuchenshi this seems to be an emulator-only issue with clear reproduction steps, could you take a look?

@yuchenshi
Copy link
Member

This seems a lot like firebase/firebase-js-sdk#2967 where @pfiadDi reported a similar symptom. This may be a bug in the Firestore Emulator.

Another repro and instructions (credits to @pfiadDi): https://github.com/pfiadDi/firestore-snapshot-bug

I use parcelJS to pack it (since parcel builds the files into a folder called dist I set the public folder at firebase init to dist). So after you build it with parcel index2.html you can close the parcel dev server again and start the emulators:

  • open the page
  • login
  • add data with a click on "Click to add data"
  • delete one doc with a click on "This deletes doc1"

The error is caught in the snapshot function and logged in the console.

@yuchenshi
Copy link
Member

I can now reproduce this against the Firestore emulator and I have a smaller repro that does not use Firebase Auth. Let me dig more into the the Firestore emulator to see why rules are evaluated that way.

@yuchenshi
Copy link
Member

Fix coming in the next release.

yuchenshi added a commit that referenced this issue May 20, 2020
@samtstern
Copy link
Contributor

Fixed by @yuchenshi in #2259 and will be included in the next release

yuchenshi added a commit that referenced this issue May 20, 2020
@zanona
Copy link

zanona commented Dec 11, 2020

I'm afraid this is still an issue when deleting the document from the emulator Firestore dashboard.
When the document gets deleted from there Null value error is thrown.

*Edit: Never mind, it looks like that when read access is given based on resource, resource == null also needs to be allowed.

@alekseykarpenko
Copy link

@zanona Thx for your update, it helped and saved time for me

@ramtinsoltani
Copy link

@zanona I had this issue with the emulator too. Thanks for the hint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants