prescottprue / react-redux-firebase

Redux bindings for Firebase. Includes React Hooks and Higher Order Components.
https://react-redux-firebase.com
MIT License
2.55k stars 559 forks source link

"useFirestoreForProfile" leads to trigger update twice with a preceding async document delete #1176

Open bookhahn opened 2 years ago

bookhahn commented 2 years ago

Hi!

I want to delete and update a couple of docs in a redux action, but I have boiled down the problem to the following:

const rrfConfig = {
    userProfile: "users",
    enableClaims: true,
    useFirestoreForProfile: true,
    attachAuthIsReady: true,
};

export const deleteEmployee = async (employee, profile) => {
  const employeeRef = firestore.collections("employees").doc(employee.id);
  const accountRef = firestore.collections("users").doc(profile.uid);
  try {
    await employeeRef.delete();
    await accountRef.update({count: firebase.firestore.FieldValue.increment(1)});
  } catch (error) {
    console.error(error.message);
  }
}

If I run the above code, the user document gets updated twice leading to: profile.count == 2

If I don't set "useFirestoreForProfile", it works as expected: user.count == 1

If I don't have another document delete or update before updating the user document, it does work, too. (even with "useFirestoreForProfile" == true) Even if I am running this from outside via node it works as expected. That is why I am considering this as a bug.

Note: I am using firebase emulators.

firebase@10.5.0 react-redux-firebase@3.3.1