I am trying to test whether a document exists with title uid. I have confirmed that the document exists in Firestore within the users collection, but doc.exists always returns false. Note that my document uid contains only collections (no fields).
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.
// ...
}
I am trying to test whether a document exists with title uid. I have confirmed that the document exists in Firestore within the users collection, but doc.exists always returns false. Note that my document uid contains only collections (no fields).
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.
// ...
}
Share
Improve this question
edited Jan 2, 2018 at 23:29
Frank van Puffelen
600k85 gold badges889 silver badges859 bronze badges
asked Jan 2, 2018 at 22:15
astrojams1astrojams1
1,5912 gold badges16 silver badges29 bronze badges
2
-
does your
console.log(doc)
print anything? – Andrew L Commented Jan 2, 2018 at 22:21 - Yes it prints a documentSnapshot object with exists property set to false – astrojams1 Commented Jan 2, 2018 at 22:28
1 Answer
Reset to default 13This happens because while you had created collections within the document, you never created the document itself. You can tell whether documents exist because if they dont they will show up in Firestore as italics.