对于类中的项目,我们需要通过Google提供程序创建帐户。一旦登录,你就可以创建项目等,并将它们保存到你的帐户中。问题是,一旦你重新登录,我用来创建新用户的setDoc
可能会重叠我的getDoc
功能。我的老师也指定不要使用get()或find()出于某种原因…谢谢你!
这是我尝试过的,但没有得到任何好的结果:
const docRef = doc(db, 'membres', creds.user.uid);
//faire une condition si le id existe dans la base de donner
//si oui, getdoc
//sinon, setdoc
if (docRef.exists) {
console.log('it exists!');
await getDoc(docRef);
} else {
console.log('No such document exists!');
await setDoc(docRef, {
nom: creds.user.displayName,
email: creds.user.email,
projets: [],
contacts: []
});
}
doc()
函数返回一个DocumentReference并且不获取文档或者检查它是否存在。您必须首先使用getDoc(docRef)
来获取文档,然后检查它是否存在。
由于不允许使用getDoc()
,所以只能在用户第一次登录时添加文档(使用setDoc()
)。试一试:
const result = await signInWithPopup(auth, provider);
const {isNewUser} = getAdditionalUserInfo(result) // <-- or result of signInWithRedirect();
if (isNewUser) {
// user has signed in first time
// setDoc(...)
} else {
// Existing user, document created already.
}