Skip to content

Firestore: Invalid result returned from getDoc after writing to cache #6739

@KoryNunn

Description

@KoryNunn

[REQUIRED] Describe your environment

  • Operating System version: Windows WSL2 (but all)
  • Browser version: Any (also node)
  • Firebase SDK version: Reproduced in 9.13.0, 9.10.x
  • Firebase Product: firestore

[REQUIRED] Describe the problem

After writing a document via setDoc(ref, data, { merge: true }), if a write is pending, getDoc will return only the updated fields from the merge, not including any existing data for that document.

Steps to reproduce:

  1. Write an update to a document with setDoc and { merge: true }, don't await the write.
  2. Read the same document immediately.
  3. Only the updated fields will be returned.

Relevant Code:

const { disableNetwork, getFirestore, getDoc, setDoc, doc } = require('firebase/firestore');

const config = {
    "firestoreEmulatorPort": 9098,
    "firebase": {
      "projectId": "<<insert>>",
      "apiKey": "<<insert>>",
      "appId": "<<insert>>",
    }
}

const app = initializeApp(config.firebase);
const db = getFirestore(app);

async function run () {

  // Consistent write to server
  await setDoc(doc(db, "test/foo"), {
      first: "Ada",
      last: "Lovelace",
      born: 1815
  });

  // Get from server
  const snapshot1 = await getDoc(doc(db, "test/foo"));
  console.log(snapshot1.metadata); // SnapshotMetadata { hasPendingWrites: false, fromCache: false }
  console.log(snapshot1.data()); // { last: 'Lovelace', born: 1815, first: 'Ada' }

  // Force opperations against the local cache.
  await disableNetwork(db);

  // Write to cache
  const write = setDoc(doc(db, "test/foo"), {
      foo: "bar"
  }, { merge: true });

  // Get from cache while the write is pending
  const snapshot2 = await getDoc(doc(db, "test/foo"));
  console.log(snapshot2.metadata); // SnapshotMetadata { hasPendingWrites: true, fromCache: true }
  console.log(snapshot2.data()); // returns { foo: 'bar' } -- Expected to return { last: 'Lovelace', born: 1815, first: 'Ada', foo: 'bar' }
}

run();

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions