我正在使用vuex-easy-firestore,我想知道如何处理交易。
这是我与Vanila Firestore处理交易的方式:
db.runTransaction(t => {
let companyRef = db.collection('companies').doc(user.company)
return t.get(companyRef)
.then(doc => {
let currentBillNumber = doc.data().currentBillNumber + 1
t.update(companyRef, {currentBillNumber: currentBillNumber})
billDocRef = db.collection('companies/' + user.company + '/bills').doc()
newBillState = {
firstname: this.firstname,
lastname: this.lastname,
emailaddress: this.emailaddress,
phonenumber: this.phonenumber,
amount: this.amount,
items: this.items,
slug: slug,
timestamp: new Date().getTime(),
type: process.env.NODE_ENV,
vehiclenumber: this.vehiclenumber,
sequenceNumber: currentBillNumber
}
t.set(billDocRef, newBillState)
})
}).then(result => {
console.log('incrementAndUpdateBill Transaction success!')
}).catch(err => {
console.log('incrementAndUpdateBill Transaction failure:', err)
})
}