代码之家  ›  专栏  ›  技术社区  ›  Steven

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

  •  1
  • Steven  · 技术社区  · 6 年前

    所以我在跟踪 Savvy Apps 学习Vue.js的教程。本教程使用Firebase和Firestore。由于Firestore是Beta版(正如教程中所说),所以可能会发生更改——我认为这里可能就是这样。

    无论如何,我想注册一个新用户。我填写表单并单击“注册”,收到以下错误消息:

    错误:函数CollectionReference.doc()要求其第一个参数为string类型,但它是:未定义的

    但是在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
        })
      },
    

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

    1 回复  |  直到 6 年前
        1
  •  4
  •   Bob Snyder    6 年前

    createUserWithEmailAndPassword() 返回 Promise 含有 UserCredential . UserCredential 有财产 user 对于 firebase.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
        })
      },