2

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.

  • In your logs appears that the doc creation happened after 10-20 minutes? In your code are you able to check that the request is made to firestore at the time the user creation? I ask this because I suspect this could be something else rather than with firestore since the issue does not happen with other operations and because it is kind of strange that if the request is made, firestore "decides" to wait 20 minutes. – Puteri Jun 26 '20 at 00:46
  • I'm logging some message to the console, so I can check when some promise is retrieved. The first one is before I call firestore in the first THEN method, and the second before dispatch in the second THEN method. So, when I try to signup, the first message appears instantly in the console. The second message does not appears. It creates a new user in firebase auth, retrieve a response, but when I call firestore, it gets stuck there and does not retrieve anything for a while. I did not wait to see the second message, because it took a long time to create a new document. – Bernardo Fonseca Jun 26 '20 at 14:11
  • If I try to create two new users in sequence (create the first, logout, then create the second), it works. When I sign the second user, its document is instantly created and then the document of the first suddenly appears. – Bernardo Fonseca Jun 26 '20 at 14:20
  • Thanks for the answer. I could take this snippet and it worked without delay. Since the other functionalities work well, there could be something else in the code that prevents the update in Firestore – Puteri Jun 26 '20 at 23:37
  • I was just learning how to do this, and my code is identical to yours, exactly...followed the same tutorial you did maybe lol. Anyway, when I create a new user I watched my database on my other monitor and the new user appeared immediately. It has to be something else going on and not what you have in that action code above – Mark B Jun 29 '20 at 11:44
  • Yeah, maybe the same tutorial. Did you get that warning message? It's is weird, because I updated react-redux-firebase and react-redux, then I needed to change part of code concerning firebase and I still get the same problem. At least I got rid of most warnings. – Bernardo Fonseca Jun 29 '20 at 14:15

2 Answers2

0

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.

Milton
  • 1
  • Hey. yeah, I followed the net ninja tutorial. I'm using his tutorial as reference to this project. I added some semicolons at the end of each line as you suggested, but I still have the same problem. It's really weird, because I really followed the tutorial when dealing with firebase. Then I updated firebase, changed the syntax to the new one and nothing done. Maybe it's my react version, because I still get a warning and it has something to do with some react update and async code. – Bernardo Fonseca Jul 06 '20 at 23:05
0

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.