Christopher Kiessling
Christopher Kiessling

Reputation: 413

AngularFire getAll in transaction, multiple get calls

I'm using the AngularFire 5.1.0 and I need to perform multiple get calls in a transaction, like so:

let firstDoc = firestore.doc('col/doc1');
let secondDoc = firestore.doc('col/doc2');
let resultDoc = firestore.doc('col/doc3');

firestore.runTransaction(transaction => {
  return transaction.getAll(firstDoc, secondDoc).then(docs => {
    transaction.set(resultDoc, {
      sum: docs[1].get('count') + docs[2].get('count')
    });
  });
});

https://cloud.google.com/nodejs/docs/reference/firestore/0.13.x/Transaction?authuser=0#getAll

but I can't see getAll method so seems like I can do multiple writes but just one get call per transaction... any idea?

Upvotes: 1

Views: 423

Answers (1)

user10747134
user10747134

Reputation:

You can leverage the async/await syntax to get the documents inside of the transaction, then just return your write.

Should be something like:

const firstDoc = firestore.doc('col/doc1');
const secondDoc = firestore.doc('col/doc2');
const resultDoc = firestore.doc('col/doc3');

firestore.runTransaction(async function(transaction) {
    const first = await firstDoc.get();
    const second = await secondDoc.get();

    return transaction.set(resultDoc, {
        sum: first.get('count') + second.get('count'),
    });
});

Upvotes: 2

Related Questions