In the code below, the FireStore is succeeding partially. It means that not all write operations are successful but some are. Please run it and see for yourself.
The cause of the error is foo.bar
foo is not defined. How to tell FireStore that if any errors happen, abort everything?
testTransaction(){
var country = 'USA';
var sfDocRef = db.collection("cities").doc("SF");
var usaDocRef = db.collection("country").doc(country); //country is undefined
db.runTransaction(function(transaction) {
return transaction.get(sfDocRef).then(function(sfDoc) {
if (!sfDoc.exists) {
throw "Document does not exist!";
}
// console.log(sadf.t)
var newPopulation = sfDoc.data().population + 1;
if (newPopulation <= 10000000) {
transaction.set(sfDocRef, { population: newPopulation }, {merge:true}); //Will succeed
console.log(foo.bar) //ERROR is here
transaction.update(usaDocRef, { lastPopulationUpdate: new Date()}); //Will fail
return newPopulation;
} else {
return Promise.reject("Sorry! Population is too big.");
}
}).catch(e => {
console.log(e)
})
}).then(function(newPopulation) {
console.log("Population increased to ", newPopulation);
}).catch(function(err) {
// This will be an "population is too big" error.
console.error(err);
});
}
To generate test data use this:
addDummy(){
// Get a new write batch
var batch = db.batch();
var sfRef = db.collection("cities").doc("SF");
batch.set(sfRef, {"population": 1000000});
var laRef = db.collection("country").doc("USA");
batch.set(laRef, {"name": 'United States of America'})
batch.commit().then(function () {
});
}