Reputation: 578
I'm trying to check if certain field in a collection has a value.
I have a collection of users and that collection has a documents by their uid than that document has a field bio.
I want to check if that field is empty or not.
This is where i'm at at the moment...
const docRef = db.collection('users').doc(auth.user.uid).get()
docRef.then((querySnapshot) => {
if(querySnapshot.docs[0].data() != ''){
//there is value in field
}
})
Upvotes: 0
Views: 186
Reputation: 83068
By doing db.collection('users').doc(auth.user.uid).get()
you define a Promise that resolves with a DocumentSnapshot
(and not a QuerySnapshot
). A DocumentSnapshot
does not have any docs
property.
So you need to do as follows:
const docRef = db.collection('users').doc(auth.user.uid).get();
docRef.then((docSnapshot) => {
if (docSnapshot.exists) {
if (docSnapshot.data().bio === '') {
// Bio is empty
}
} else {
// docSnapshot.data() will be undefined in this case
console.log("No such document!");
}
}).catch((error) => {
console.log("Error getting document:", error);
});
Note that from a naming convention perspective, it would make more sense to write:
const docRef = db.collection('users').doc(auth.user.uid); // This is a DocumentReference
docRef.get().then((doc) => {...});
Upvotes: 1