Skip to content

runTransaction isn't awaited #6915

Closed
Closed
@Lyokone

Description

@Lyokone

First reported here: firebase/flutterfire#10153

[REQUIRED] Describe your environment

  • Operating System version: macOS Latest
  • Browser version: Chrome latest
  • Firebase SDK version: 9.15
  • Firebase Product: firestore

[REQUIRED] Describe the problem

Firestore doesn't retrieve the latest data when using 'getDoc' on a document just after this document was modified through a transaction and a stream to listen on the same document is active.

The content of the retrieved document in the 'getDoc' call should be updated.
This issue is not happening if we are not listening to the document OR if the document is not updated through a transaction.

Steps to reproduce:

It happens on true Firebase instances but is harder to reproduce locally on an emulator.

Relevant Code:

import { initializeApp } from "firebase/app";
import {
  doc,
  getDoc,
  getFirestore,
  onSnapshot,
  runTransaction,
} from "firebase/firestore";

var config = {
  ...
};

// Initialize Firebase
var app = initializeApp(config);
const db = getFirestore(app);

const test_doc = doc(db, "test_10153", "test");

const unsub = onSnapshot(test_doc, (doc) => {
  console.log("Current data: ", doc.data());
});

for (let i = 0; i < 10; i++) {
  var newPopulation;

  await runTransaction(db, async (transaction) => {
    const sfDoc = await transaction.get(test_doc);
    if (!sfDoc.exists()) {
      throw "Document does not exist!";
    }

    newPopulation = sfDoc.data().population + 1;
    console.log("Updating to:", newPopulation);
    transaction.update(test_doc, { population: newPopulation });
  });

  const data = await getDoc(test_doc);

  console.log("Expected: ", newPopulation);
  console.log("Actual: ", data.data().population);

  console.log("---------");
}

unsub();

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions