我正在尝试创建一个文档,其 ID 是用户注册时的 ID
这是用户注册时运行的函数 ->
const register = (e) => {
e.preventDefault();
auth
.createUserWithEmailAndPassword(email, password)
.then((auth) => {
if (auth) {
db.collection("users").add({
email: email,
password: password,
});
history.push("/");
}
})
.catch((error) => alert(error.message));
};
任何人都可以帮助我如何传递 .doc 引用来完成此任务吗?
最佳答案
你能试试下面的代码吗?
const register = e => {
e.preventDefault();
auth
.createUserWithEmailAndPassword(email, password)
.then(credential => {
if (credential && credential.user) {
db.collection("users")
.doc(credential.user.uid)
.set({
email: email,
password: password
});
history.push("/");
}
})
.catch(error => alert(error.message));
};
参见:
关于reactjs - 如何在 Firebase firestore 中使用用户 'uid' 创建文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64077214/