【发布时间】:2020-10-27 18:13:14
【问题描述】:
我正在为已登录的用户创建个人资料。我遇到的问题是,在某些情况下允许为给定的 uid 创建重复的配置文件,而不是返回已经存在的配置文件。
const unsubscribe = firebase.auth().onAuthStateChanged((user) => {
if (user) {
findProfile(user.uid)
.then((profile) => console.log(profile))
.catch((error) => createProfile(user.uid))
} else {
console.log("user not signed in")
}
});
export const findProfile = (uid) => {
return firebase
.collection("profiles")
.where("uid", "==", uid)
.get()
.then((querySnapshot) => {
if (querySnapshot.size > 0) {
return querySnapshot.docs[0];
} else {
throw new Error("profile not found");
}
});
};
export const createProfile = (uid) => {
return firebase.collection("profiles").add({uid});
};
【问题讨论】:
标签: javascript firebase google-cloud-firestore firebase-authentication