在正确创建用户后,Firebase Firestore自定义ID无法创建新文档

时间:2020-06-28 12:43:47

标签: javascript firebase google-cloud-firestore firebase-authentication

我在Vue应用程序中创建了一个样本注册表单,以自动创建一个Firestore文档,并附加了带有自定义文档ID的用户UID。

用户创建成功,但是即使使用catch()错误方法,该文档也未创建,并且在控制台上也不显示任何错误。

register() {
  //Generate pin
  function generateQuickGuid() {
    return Math.random()
      .toString(36)
      .substring(2, 15);
  }
  let ref = fs.doc(generateQuickGuid());
  ref.get().then(doc => {
    if (doc.exists) {
      console.log("Pin Exists");
    } else {
      console.log("pin doesnt exists");
      // then add user to firestore
      if (
        this.email &&
        this.password &&
        this.displayName &&
        this.category
      ) {
        auth
          .createUserWithEmailAndPassword(this.email, this.password)
          .then(cred => {
            ref
              .set({
                Name: this.displayName,
                address: "close to understanding, firebase, auth",
                phone: "09808763987",
                category: this.category,
                alias: pin,
                user_id: cred.user.uid
              })
              .catch(e => {
                console.log(e);
              });

            console.log("User Added");
          })
          .catch(error => {
            // Handle Errors here.
            var errorCode = error.code;
            var errorMessage = error.message;
            // ...
            console.log(errorCode, errorMessage);
          });
      }
    }
  });
}

1 个答案:

答案 0 :(得分:1)

register() {
  //Generate pin
  function generateQuickGuid() {
    return Math.random()
      .toString(36)
      .substring(2, 15);
  }
  let ref = fs.doc(generateQuickGuid());
  ref.get().then(doc => {
    if (doc.exists) {
      console.log("Pin Exists");
    } else {
      console.log("pin doesnt exists");
      // then add user to firestore
      if (
        this.email &&
        this.password &&
        this.displayName &&
        this.category
      ) {
        AuthResult authResult = auth
          .createUserWithEmailAndPassword(this.email, this.password)
          .catch(error => {
            // Handle Errors here.
            var errorCode = error.code;
            var errorMessage = error.message;
            // ...
            console.log(errorCode, errorMessage);
          });

       ref.set({
                Name: this.displayName,
                address: "close to understanding, firebase, auth",
                phone: "09808763987",
                category: this.category,
                alias: pin,
                user_id: authResult.getUser().getUid()
              })
              .catch(e => {
                console.log(e);
              });
      }
    }
  });
}

尝试一下