Skip to content
This repository was archived by the owner on Mar 4, 2026. It is now read-only.
This repository was archived by the owner on Mar 4, 2026. It is now read-only.

Firebase transactions with read operations after write operations #1482

Description

@Veargan

Environment details

  • npm version: 6.14.6
  • Node.js version: 12.8.3
  • node engine: 12
  • @google-cloud/firestore version: 4.5.0
  • firebase-admin: 9.6.0
  • firebase-functions: 3.13.2

Steps to reproduce

Docs states that you can execute reads after writes, but as it turns out you cannot. Is that a bug or misleading documentation?

  1. Call a cloud function or trigger background function
const onFirestoreTestsWrite = functions.firestore
  .document('tests/{testId}')
  .onWrite(async (change, context) => {
    await admin.firestore().runTransaction(async (t) => {
      const testDoc = await admin.firestore().collection('tests').doc().get();
      t.set(testDoc.ref, {});
      t.get(testDoc.ref);
    });
  });
const tests_get = async (req, res) => {
  try {
    const test = await admin.firestore().runTransaction(async (t) => {
      const testDoc = await admin.firestore().collection('tests').doc().get();
      t.set(testDoc.ref, {});
      return t.get(testDoc.ref);
    });
    res.send({id: test.id});
  } catch (e) {
    res.status(400).send({ error: e['message'] });
  }
};

Will result in error:

Error: Firestore transactions require all reads to be executed before all writes.
    at Transaction.get (/workspace/node_modules/@google-cloud/firestore/build/src/transaction.js:76:19)
    at /workspace/lib/firestore/tests-write.js:46:11
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Transaction.runTransaction (/workspace/node_modules/@google-cloud/firestore/build/src/transaction.js:323:26)
    at async /workspace/lib/firestore/tests-write.js:43:5 

Metadata

Metadata

Assignees

Labels

api: firestoreIssues related to the googleapis/nodejs-firestore API.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.type: docsImprovement to the documentation for an API.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions