firebase / firebase/firebase-tools

[Firestore Emulator] onSnapshot triggered with a nonexistent doc on doc creation

Open
#3,867 4 comments 0 reactions 0 assignees View on GitHub
emulator-suite emulators: firestore Needs: Attention type: bug
Dominant language
TypeScript
Stars
4.5k
Forks
1.3k
Avg merge
1d 12h
Merged PRs (30d)
84

Description

### [REQUIRED] Environment info

**firebase:** 9.1.2

**firebase-tools:** 9.21.0

**Platform:** macOS Big Sur (11.6)

### [REQUIRED] Test case

I'm experiencing a bug using the Firestore Emulator. On document creation, the `onSnapshot` listener (that was set up beforehand) is triggered twice:
- The first time with the expected document (e.g. the one that was just created). It has the `hasPendingWrites` metadata property set to `true`, which is expected.
- The second time with an empty document (`snapshot.exists()` is `false`), whereas the document was successfully created and is visible in the Emulator UI. The `hasPendingWrites` metadata is `false` in this case, which is expected. If I refresh the page however, the document is correctly fetched.

This never happened to me in production so I think it's an emulator-specific issue. Also, it seems to happen only once, when the emulator was just started. In other words, subsequent document creations seem not to be affected by this bug.

### [REQUIRED] Steps to reproduce

N/A

### [REQUIRED] Expected behavior

I'm expecting the snapshot attached the second `onSnapshot` triggering (the `hasPendingWrites: false` one) to exist and its data to match the created document.

### [REQUIRED] Actual behavior

See above.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.