angular - 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不一样。
解决方案
而不是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();
推荐阅读
- google-cloud-platform - BigQuery 通过 SQL 导出到 CSV 文件
- html - VBA 无法访问 HTML 中的嵌套 span 元素
- python - 为什么 python mysql 连接器不接受登录作为变量?我该如何解决?
- sql - 使用“in”和两个索引之间的数组进行查询时有什么区别?
- python - 有没有办法用下划线(或任何其他符号)替换字符串中的单个空格?
- java - 有没有办法将 java 应用程序添加到我的 github 网站,以便我的个人网站有应用程序的现场演示?
- azure - Azure 通知中心是否可供使用?
- sql-server - 列名或提供的值的数量与使用 sql server 的表定义不匹配
- python - 如何在sql中存储一种特殊的数据
- scala - 为什么总是存在
在scala中使用printWriter或BufferedWriter时在我的输出文件中?