我正在尝试测试是否存在标题为 uid 的文档。我已确认该文档存在于用户集合中的 Firestore 中,但 doc.exists 始终返回 false。请注意,我的文档 uid 仅包含集合(无字段)。
firebase.auth().onAuthStateChanged((user) => {
if (user) {
// User is signed in.
var uid = user.uid
db.collection("users").doc(uid)
.get()
.then((doc) => {
console.log(doc)
if (doc.exists) {
console.log('returning user')
} else {
console.log('new user')
}
}).catch(function(error) {
console.log("Error getting document:", error);
});
} else {
// User is signed out.
// ...
}
最佳答案
发生这种情况是因为虽然您在文档中创建了集合,但您从未创建过文档本身。您可以判断文档是否存在,因为如果不存在,它们将以斜体显示在 Firestore 中。
关于javascript - Firebase - 文档存在但 doc.exists 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48068581/