简体   繁体   中英

runTransaction: Provided document reference is from a different Firestore instance error

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

Okay, so I found the error. I was using the wrong reference in this line:

transaction.update(currentSetNo, { highestCount: setNo });

which should actually be

transaction.update(
                    doc(db, "users", userStatus.uid, "set", "COUNTER"),
                        { highestCount: setNo }
                    );

Or whatever your db reference is, in replacement of currentSetNo in the first example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM