Creating user in Firebase gives me an error

2020-03-06 20:47发布

So I'm following a Savvy Apps tutorial in order to learn Vue.js. This tutorial uses Firebase with Firestore. Since Firestore is in Beta (as the tutorial says), changes might happen - and I think that might be the case here.

In any case, I'm trying to sign up a new user. I fill out the form and click 'Sign up' and I get this error message:

Error: Function CollectionReference.doc() requires its first argument to be of type string, but it was: undefined

But looking in Firebase, I see that the user has been created. So why do I get this error message? What is the first argument?

The code for signup looks like this:

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

Let me know if you need more code - this is the first time I'm testing Vue.js.

1条回答
Animai°情兽
2楼-- · 2020-03-06 21:06

createUserWithEmailAndPassword() returns a Promise containing a UserCredential. UserCredential has a property user for the firebase.User object.

You need to make the appropriate changes to your code to correctly access the UID:

  signup() {
    this.performingRequest = true;
    fb.auth.createUserWithEmailAndPassword(this.signupForm.email, this.signupForm.password)
    .then(credential=> {  // CHANGED
      this.$store.commit('setCurrentUser', credential.user);  // CHANGED
      // create user obj
      fb.usersCollection.doc(credential.user.uid).set({  //CHANGED
        name: this.signupForm.name,
        title: this.signupForm.title
      }).then(() => {
        this.$store.dispatch('fetchUserProfile');
        this.performingRequest = false;
        this.$router.push('/dashboard')
      }).catch(err => {
        console.log(err);
        this.performingRequest = false;
        this.errorMsg = err.message
      })
    }).catch(err => {
      console.log(err);
      this.performingRequest = false;
      this.errorMsg = err.message
    })
  },
查看更多
登录 后发表回答