javascript - Firebase/javascript——如何将在firestore中创建的数据与用户连接起来
问题描述
Firebase v9
如何将在 Firestore 中创建的数据与创建此数据的用户连接起来?
我使用该createUserWithEmailAndPassword
功能在 firebase 中授权用户,授权后我使用 获取用户数据onAuthStateChanged
,但是如何将特定用户与仅由他们创建的数据连接?
解决方案
例如,要为特定用户创建个人资料文档,常见的模式是使用用户的 UID 作为文档 ID。像这样的东西:
import { getAuth, onAuthStateChanged } from "firebase/auth";
import { doc, setDoc } from "firebase/firestore";
const auth = getAuth();
onAuthStateChanged(auth, (user) => {
if (user) {
const uid = user.uid;
await setDoc(doc(db, "users", uid), {
name: user.displayName
});
// ...
}
});
另见:
推荐阅读
- sql-server - 尝试访问 Docker 上的 SQL Server 时,在建立与 SQL Server 的连接时发生网络相关或特定于实例的错误
- reactjs - 条形图中的标签?
- hadoop - 计算一系列日期之间的日期字符串
- java - jooq 使用 mapInto 从多个表中选择
- angular - 无法使用角材料中的 mat-tab-nav-bar 覆盖 mat-ink-bar 颜色
- python - 导入库的问题
- r - 构建函数以将自然中断jenks应用于我的df列时出错
- javascript - 通过 URL 参数设置 cookie 并在整个网站上保持其值
- excel - EXCEL VBA 运行时错误 91 - 未设置对象
- r - 使用 R `bookdown::gitbook` 我可以将数字居中但不能将标题居中吗?