Firestore transaction with multiple get

前端 未结 3 1515
一个人的身影
一个人的身影 2021-02-13 05:13

I\'m trying to run a transaction with a variable number of read operations. I put the read () operations before than update ().

Reading the Firestore doc on https://clou

3条回答
  •  你的背包
    2021-02-13 05:59

    The Firestore doc doesn't say this, but the answer is hidden in the API reference: https://cloud.google.com/nodejs/docs/reference/firestore/0.13.x/Transaction?authuser=0#getAll

    You can use Transaction.getAll() instead of Transaction.get() to get multiple documents. Your example will be:

    const reservationCol = this.db.firestore.collection('reservations');
    return this.db.firestore.runTransaction(t => {
      return t.getAll(reservationCol.doc('id1'), reservationCol.doc('id2'))
        .then(docs => {
          const id1 = docs[0];
          const id2 = docs[1];
          if (!(id1.exists && id2.exists)) {
            // do stuff
          } else {
            // throw error
          }
        })
    }).then(() => console.log('Transaction succeeded'));
    

提交回复
热议问题