Creating user in Firebase gives me an error

大兔子大兔子 提交于 2020-01-13 06:08:26

问题


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:


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
    })
  },


来源:https://stackoverflow.com/questions/51583290/creating-user-in-firebase-gives-me-an-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!