AngularFire getAll在事务中,多个get调用

时间:2019-01-15 23:56:53

标签: angular firebase transactions google-cloud-firestore angularfire

我正在使用AngularFire 5.1.0,我需要在一个事务中执行多个get调用,如下所示:

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

但是我看不到getAll方法,所以看起来我可以执行多次写入,但是每个事务只能执行一次get调用...知道吗?

1 个答案:

答案 0 :(得分:1)

您可以利用async/await syntax在交易中获取文档,然后只返回您的写信。

应该是这样的:

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'),
    });
});