boatbuyer
boatbuyer

Reputation: 13

Error performing transaction, Every document read in a transaction must also be written., null

I am receiving this error when querying a Firestore database using Flutter.

E/flutter (17558): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(Error performing transaction, Every document read in a transaction must also be written., null)

Future getFavorites(String uid) {
  DocumentReference favoritesReference =
      Firestore.instance.collection('users').document(uid);
  List favoritesList = [];

  return Firestore.instance.runTransaction((Transaction tx) async {
    DocumentSnapshot postSnapshot = await tx.get(favoritesReference);
    var length;
    if (postSnapshot.exists) {
      length = postSnapshot.data['favorites'].length;
    }

    print(length);
  });
}

Upvotes: 0

Views: 256

Answers (1)

Joss Baron
Joss Baron

Reputation: 1524

It seems you are not writing your document. Each document read in the transaction should be written. Then, you can add a set(), update() or delete() that corresponds to each of the document reads.

If you don't need to write back to favoritesReference, then read it before the transaction rather than inside the transaction.

Upvotes: 1

Related Questions