简体   繁体   中英

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.

I believe you're following The Net Ninja's series (I am too). If you follow what he did exactly ( here ) it will work. I've personally diffed yours and my implementation with his, and the only thing that was different was the semicolons. Try adding semicolons after each line. Perhaps the chaining of promises confuses the compiler?

It still won't work when you signup, then signout repeatedly. You need to refresh after each signout.

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.

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