首页 > 解决方案 > Angular Firebase 注册问题

问题描述

我正在尝试将注册用户的数据保存在 Firestore 中。在用户数据中,我将用户的 uID 保存在 firestore 中。但问题是它将用户的uid保存在firestore中。但是 docId 是不同的。我需要文档 ID 也与用户 ID 相同。这是我的代码

      signupUser(user: AppUser) {
        return new Promise((resolve, reject) => {
          this.firbaseAuth
            .auth
            .createUserWithEmailAndPassword(user.email, user.password)
            .then((signUpResponse) => {
              this.userType = user.usertype;
              localStorage.setItem('user', JSON.stringify(this.userData));
              signUpResponse.user.updateProfile({
                displayName: `${user.username}`
              }).then(() => {
                this.angularFirestore.collection('webUsers').add(user).then(ref => {
                  ref.set({ userID: this.firbaseAuth.auth.currentUser.uid }, { merge: true }).then(() => {
                  window.location.reload();
                  });
                })
                resolve();
              }).catch((err) => {
                reject(err);
              })
            }).catch((err) => {
              reject(err);
            });
        })
      }

这是图像。data 中的 userId 与身份验证 userId 中的相同。但不知道为什么docid不一样。

在此处输入图像描述

标签: angular

解决方案


而不是add(user)使用doc({id})

this.angularFirestore.collection('webUsers').doc(this.firbaseAuth.auth.currentUser.uid ).then(ref => {
                  ref.set({...user ,userID: this.firbaseAuth.auth.currentUser.uid }).then(() => {
                  window.location.reload();
                  });
                })

另一个改进我建议使用 async/await 来摆脱很多then

const DocRef = await this.angularFirestore.collection('webUsers').doc(this.firbaseAuth.auth.currentUser.uid);
await ref.set({...user ,userID: this.firbaseAuth.auth.currentUser.uid });
window.location.reload();

推荐阅读