I want to know if there is a way to check if a property is present in a Cloud Firestore document. Something like document.contains("property_name")
or if there is a document property exist.
To solve this, you can simply check the DocumentSnapshot
object for nullity like this:
var yourRef = db.collection('yourCollection').doc('yourDocument');
var getDoc = yourRef.get()
.then(doc => {
if (!doc.exists) {
console.log('No such document!');
} else {
if(doc.get('yourPropertyName') != null) {
console.log('Document data:', doc.data());
} else {
console.log('yourPropertyName does not exist!');
}
}
})
.catch(err => {
console.log('Error getting document', err);
});
I think you refer to making a query. Still there is no way to check if some field is present or not in the Firestore. But you can add another field with value true/false
val query = refUsersCollection
.whereEqualTo("hasLocation", true)
query.get().addOnSuccessListener {
// use the result
}
check out this links for more
https://firebase.google.com/docs/firestore/query-data/queries How do I get documents where a specific field exists/does not exists in Firebase Cloud Firestore?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With