在 Firebase 中创建用户给我一个错误

Ste*_*ven 2 javascript firebase vue.js firebase-authentication google-cloud-firestore

所以我正在学习Savvy Apps教程以学习 Vue.js。本教程将 Firebase 与 Firestore 结合使用。由于 Firestore 处于 Beta 版(如教程所述),因此可能会发生变化 - 我认为这里可能就是这种情况。

无论如何,我正在尝试注册一个新用户。我填写表格并单击“注册”,然后收到此错误消息:

错误:Function CollectionReference.doc() 要求它的第一个参数是字符串类型,但它是:未定义

但是在 Firebase 中,我看到用户已创建。那么为什么我会收到此错误消息?第一个论点是什么?

注册代码如下所示:

  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
    })
  },
Run Code Online (Sandbox Code Playgroud)

如果您需要更多代码,请告诉我 - 这是我第一次测试 Vue.js。

Bob*_*der 6

createUserWithEmailAndPassword()返回一个Promise包含UserCredential 的UserCredential有一个属性userfirebase.User对象。

您需要对代码进行适当的更改才能正确访问 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
    })
  },
Run Code Online (Sandbox Code Playgroud)