I'm trying to implement easier to read documentIDs and through this strategy that Frank recommended but I've run into an error that I haven't been able to fix.
I can successfully read the value of highestCount from the COUNTER document but have an error when I try to update it.
The error states:
FirebaseError: Provided document reference is from a different Firestore instance
My Javascript code (using React):
const onSetCreation = async (e) => {
e.preventDefault();
var setNo = 0;
try {
await runTransaction(db, async (transaction) => {
const currentSetNo = await transaction.get(
doc(db, "users", userStatus.uid, "set", "COUNTER")
);
console.log(currentSetNo.data());
// if theres no counter, create one
if (!currentSetNo.exists()) {
transaction.update({ highestCount: 0 });
setNo = 0;
} else {
setNo = currentSetNo.data().highestCount + 1;
// update the COUNTER once iterated
transaction.update(currentSetNo, { highestCount: setNo });
}
});
// succesful update of COUNTER
console.log("Transaction successfully committed!");
} catch (e) {
console.log(e);
}
};
This function is triggered on a button click.
My Firestore structure
is users -> [userid] -> set -> COUNTER
Where counter is a document that holds highestCount which is to be updated everytime this function runs.
I'm able to console.log the value of COUNTER, just can't update it.
I haven't been able to find any similar problems.
I'm using the Web version 9 of Firestore, I've read this documentation for runTransaction.
Cheers