angularfire2 transactions and batch writes in firestore

前端 未结 2 1030
生来不讨喜
生来不讨喜 2021-01-04 11:51

How can I do batch writes/run transactions with firestore in an angular2 app?

https://firebase.google.com/docs/firestore/manage-data/transactions

If it\'s po

2条回答
  •  悲&欢浪女
    2021-01-04 12:44

    It's simple:

    constructor(private db: AngularFirestore){}
    
    inserting(){
            //--create batch-- 
            var batch = this.db.firestore.batch();
    
            //--create a reference-- 
            const userRef = this.db.collection('users').doc('women').ref;
            batch.set(userRef , {
              name: "Maria"
            });
    
            //--create a reference-- 
            const userRef = this.db.collection('users').doc('men').ref;
            batch.set(userRef , {
              name: "Diego"
            });
    
            //--Others more 54 batch's--
    
            //--finally--
            return batch.commit();
        }
    

提交回复
热议问题