Firebase/javascript -- 如何将在 firestore 中创建的数据连接到用户
Firebase/javascript -- How to connect data created in firestore with user
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
});
// ...
}
});
另见:
- getting the current user
上的 Firebase 文档
- writing a document
上的 Firebase 文档
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
});
// ...
}
});
另见:
- getting the current user 上的 Firebase 文档
- writing a document 上的 Firebase 文档