Search code examples
reactjsfirebasegoogle-cloud-firestorereact-redux-firebaseredux-firestore

Delay in Firestore to create a new document in the users collection


I hope someone can help me with this issue.

I'm currently working on a project in which I use firebase to create new users. So, when someone sign a new user up, firebase creates a new user, then it sends a promise to firestore, so it can create a new document in a collection called 'users', so I can access some user data, as name, last name and initials.

My problem is that, when I sign a new user up, the account is instantly created in firebase, but it takes a long time to create a new document with the user data in firestore. When I say a long time, I mean 10, 20 minutes or even more. Thus, an account is created with undefined data, until firestore decide to create new document.

The described procedure is shown in the code below:

export const signUp = newUser => {
    return (dispatch, getState, { getFirebase }) => {
        const firebase = getFirebase()
        const firestore = getFirebase().firestore()

        firebase.auth().createUserWithEmailAndPassword(
            newUser.email,
            newUser.password
        ).then(resp => {
            return firestore.collection('users').doc(resp.user.uid).set({
                firstName: newUser.firstName,
                lastName: newUser.lastName,
                initials: newUser.firstName[0] + newUser.lastName[0]
            }).then(() => {
                dispatch({ type: 'SIGN_UP_SUCCESS' })
            })
        }).catch(err => {
            dispatch({ type: 'SIGN_UP_FAIL', err })
        })
    }
}

I'm using redux-firestore and react-redux-firebase as dependencies to connect my project to firebase. But it does not seem to be the problem, because firestore works seamlessly for other functionalities of the project, as add and delete new data to the user when its document is finally created, or when I try to delete an user, it also works fine.

So, I would be glad if someone could find an explanation for this delay and help me to overcome this problem.


Solution

  • After some time I realized that the problem was not the code or firebase. The problem was something in the structure of the project it self.

    In order to solve this problem, I needed to get rid of

    "@testing-library/jest-dom"
    "@testing-library/react"
    "@testing-library/user-event"
    

    In package.json. Then install the dependencies again.