VueJS Firebase语法错误

时间:2018-08-06 23:07:23

标签: firebase firebase-realtime-database vue.js vuejs2 firebase-authentication

在此注册方法中找不到语法错误。现在已经一个多小时了。

.then(() => {
            db.collection("users").doc(this.slug).get().then((doc) => {

                let data = doc.data()
                db.collection("users").doc(cred.user.uid).set(data).then({
                  db.collection("users").doc(this.slug).delete()
                })

            })
          })

上面的代码基本上获取了新创建的文档,然后将数据放入let data中。之后,它将使用用户UID创建一个新文档,因为该名称会将数据传递给它,然后仅删除旧文档。该代码中存在语法错误,但指示器指出,这是db和collection(db.collection)之间的点。

Error report

methods: {
    signup(){
      console.log('signup ran')
      if(this.heroName){
        this.slug = slugify(this.heroName, {
          replacement: '-',
          remove: /[$*_+~.()'"!\-:@]/g,
          lower: true
        })
        console.log(this.slug)
        let ref = db.collection('users').doc(this.slug)
        ref.get().then(doc => {
          if(doc.exists){
            this.feedback = 'This alias already exists'
          } else {
            // this alias does not yet exists in the db
            this.feedback = 'This alias is free to use'
            firebase.auth().createUserWithEmailAndPassword(this.email, this.password)
            .then(cred => {
              ref.set({
                alias: this.heroName,
                user_id: cred.user.uid,
                gemcount: 15
              })
              // FIXME: error below
              .then(() => {
                db.collection("users").doc(this.slug).get().then((doc) => {

                    let data = doc.data()
                    db.collection("users").doc(cred.user.uid).set(data).then({
                      db.collection("users").doc(this.slug).delete()
                    })

                })
              })
              .then(() => {
                this.$router.push({ name: 'Core' })
              })
            })
            .catch(err => {
              console.log(err)
              this.feedback = err.message;
            })
          }
        })
      } else {
        this.feedback = 'Please enter a heroName'
      }
    }
  }

1 个答案:

答案 0 :(得分:1)

db.collection("users").doc(cred.user.uid).set(data).then({
     db.collection("users").doc(this.slug).delete()
})

then希望它的参数是一个函数,我认为它是您错误的出处,正确的应该是:

db.collection("users").doc(cred.user.uid).set(data).then(() => {
     db.collection("users").doc(this.slug).delete()
})