Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chained promises not working #3146

Closed
evtlucas opened this issue Jun 2, 2020 · 2 comments
Closed

Chained promises not working #3146

evtlucas opened this issue Jun 2, 2020 · 2 comments

Comments

@evtlucas
Copy link

evtlucas commented Jun 2, 2020

[REQUIRED] Describe your environment

  • Operating System version: Ubuntu 18.04
  • Browser version: Firefox 76.0.1
  • Firebase SDK version: 7.14.6
  • Firebase Product: firestore (auth, database, storage, etc)

[REQUIRED] Describe the problem

I'm trying to create a document inside a collection called users from "then" inside fb.auth.createUserWithEmailAndPassword. There is a doc.set inside this "then". The second "then" is not working, and it makes the page freezes. If I make some change in the code, the "reactivitity" unfreezes the page and I can get the document created after I click on a link that calls fb.auth.signOut. It seems that the inner "then" promise is pending.

Steps to reproduce:

Relevant Code:

signup () {
  this.performingRequest = true
  return fb.auth.createUserWithEmailAndPassword(this.signupForm.email, this.signupForm.password).then(user => {
    this.$store.commit('setCurrentUser', user.user)
    // create user obj
    const userProfile = {
      name: this.signupForm.name,
      title: this.signupForm.title
    }
    const promise = fb.usersCollection.doc(user.user.uid).set(userProfile)
    return promise.then(() => {
      this.$store.dispatch('fetchUserProfile')
      this.performingRequest = false
      this.$router.push('/dashboard')
    }).catch(err => {
      this.performingRequest = false
      console.log(err)
      this.errorMsg = err.message
    })
  }).catch(err => {
    this.performingRequest = false
    console.log(err)
    this.errorMsg = err.message
  })
}

https://stackblitz.com/fork/firebase-issue-sandbox

// TODO(you): code here to reproduce the problem
@tylerhahn
Copy link

I ran into this error as well.
In order to this working again, I had to use async awaits instead of .then();

@evtlucas evtlucas closed this as completed Jun 4, 2020
@evtlucas evtlucas reopened this Jun 4, 2020
@evtlucas
Copy link
Author

evtlucas commented Jun 4, 2020

Thanks for the help! It worked!

Here is the solution:

  async signup () {
      this.performingRequest = true
      await fb.auth.createUserWithEmailAndPassword(this.signupForm.email, this.signupForm.password).then(user => {
        this.$store.commit('setCurrentUser', user.user)
      })
      const user = this.$store.getters.getCurrentUser
      const userProfile = {
        name: this.signupForm.name,
        title: this.signupForm.title
      }
      fb.usersCollection.doc(user.uid).set(userProfile).then(() => {
        this.$store.dispatch('fetchUserProfile')
        this.performingRequest = false
        this.$router.push('/dashboard')
      }).catch(err => {
        this.performingRequest = false
        console.log(err)
        this.errorMsg = err.message
      })
    }

@evtlucas evtlucas closed this as completed Jun 4, 2020
@firebase firebase locked and limited conversation to collaborators Jul 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants