Answers for "firebase firestore async await transactions"

0

firebase firestore async await transactions

try {
    await db.runTransaction(async transaction => {
      transaction
        .update(
          db.collection("col1").doc(id1),
          dataFor1
        )
        .update(
          db.collection("col2").doc(id2),
          dataFor2
        );
    });
  } catch (err) {
    throw new Error(`Failed transaction: ${err.message}`);
  }
Posted by: Guest on August-19-2021
0

firebase firestore async await transactions

var docRef = admin.firestore().collection("docs").doc(docId);
let doc = await admin.firestore().runTransaction(t => t.get(docRef));

if (!doc.exists) {throw ("doc not found");}
var newLikes = doc.data().likes + 1;

await doc.ref.update({ likes: newLikes });
Posted by: Guest on August-19-2021
0

firebase firestore async await transactions

try{
   await db.runTransaction(async transaction => {
       const doc = await transaction.get(ref);
       if(!doc.exists){
            throw "Document does not exist";
       }
       const newCount = doc.data().count + 1;
       transaction.update(ref, {
           count: newCount,
       });
  })
} catch(e){
   console.log('transaction failed', e);
}
Posted by: Guest on August-19-2021

Browse Popular Code Answers by Language