firebase / firebase/firebase-js-sdk

Transaction with read operation after write operation works despite error

Open
#7,714 2 comments 0 reactions 1 assignee Claimed by @MarkDuckworth View on GitHub
api: firestore feature request question
Dominant language
TypeScript
Stars
5.1k
Forks
1k
Avg merge
2d 21h
Merged PRs (30d)
37

Description

### Operating System

macOS

### Browser Version

Safari 16.5.2

### Firebase SDK Version

10.5.0

### Firebase SDK Product:

Firestore

### Describe your project's tooling

index.html with source tag. Just javascript.

### Describe the problem

A transaction is said [to now allow](https://firebase.google.com/docs/firestore/manage-data/transactions#transaction_failure) a read operation after a write operation. Indeed, if one attempts to do so, an error is thrown `Unhandled Promise Rejection: FirebaseError: Firestore transactions require all reads to be executed before all writes.`. However, the write successfully completes regardless, and the last paragraph says

> A failed transaction returns an error and does not write anything to the database. You do not need to roll back the transaction; Cloud Firestore does this automatically.

The transaction is **not** rolled back, when it should, according to description.

### Steps and code to reproduce issue

```
const db = getFirestore();
const citiesRef = collection(db, "cities");

await setDoc(doc(citiesRef, "SF"), {
name: "San Francisco", state: "CA", country: "USA",
capital: false, population: 860000,
regions: ["west_coast", "norcal"]
});

const sfDocRef = doc(db, "cities", "SF");

try {
await runTransaction(db, async (transaction) => {
transaction.update(sfDocRef, { population: 20 }).get(sfDocRef);//throws error - document is updated regardless
});
} catch (e) {
console.error(e);
}
```

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.