Reputation: 319
I am checking whether the Boolean field called attending
exists, however I am not sure how to do that.
Is there a function such as .child().exists()
or something similar that I could use?
firebaseFirestore.collection("Events")
.document(ID)
.collection("Users")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()) {
for(QueryDocumentSnapshot document: task.getResult()){
attending = document.getBoolean("attending");
}
}
}
});
Upvotes: 11
Views: 25590
Reputation: 382
You can use hasOwnProperty
if (docSnap.exists()) {
...
if (docSnap.data().hasOwnProperty('myField')) {
...
} else {
console.log("No such field!");
}
} else {
console.log("No such document!");
}
Upvotes: 0
Reputation: 95
How about using:
QueryDocumentSnapshot<Map<String, dynamic>> data = 'some data';
if (data.data().containsKey(fieldName)) {
// do something.
}
Upvotes: 0
Reputation: 1078
instead of documentSnapshot.get("attending"); do this documentSnapshot.ContainsField("attending");
If the field exists it will return true else returns false.
Upvotes: 0
Reputation: 1
I didn't find any solution so what I do is a try - catch (sorry Flutter / Dart, not java):
bool attendingFieldExists = true;
DocumentSnapshot documentSnapshot =
// your Document Query here
await FirebaseFirestore.instance
.collection('collection')
.doc(FirebaseAuth.instance.currentUser?.uid)
.get();
// trying to get "attending" field will throw an exception
// if not existing
try {
documentSnapshot.get("attending");
} catch (e) {
attendingFieldExists = false;
print("oops.. exception $e");
}
Then you can adapt the code you want to apply depending on attendingFieldExists
.
I'm new to Flutter / Dart so not sure this is the best way to handle this.. but it works for me.
Upvotes: 0
Reputation: 170
I use following boolean method may be someone else can
for(QueryDocumentSnapshot document: task.getResult()){
if(document.contains("attending")){
attending = document.getBoolean("attending");
}
}
Upvotes: 0
Reputation: 53
Here is how you can achieve it or you may have resolved already, this for any one searches for solution.
As per the documentation:
CollectionReference citiesRef = db.collection("cities");
Query query = citiesRef.whereNotEqualTo("capital", false);
This query returns every city document where the capital field exists with a value other than false or null. This includes city documents where the capital field value equals true or any non-boolean value besides null.
For more information : https://cloud.google.com/firestore/docs/query-data/order-limit-data#java_5
Upvotes: 0
Reputation: 1026
You can check firestore document exists using this,
CollectionReference mobileRef = db.collection("mobiles");
await mobileRef.doc(id))
.get().then((mobileDoc) async {
if(mobileDoc.exists){
print("Exists");
}
});
Upvotes: -3
Reputation: 80914
You can do the following:
if(task.isSuccessful()){
for(QueryDocumentSnapshot document: task.getResult()){
if (document.exists()) {
if(document.getBoolean("attending") != null){
Log.d(TAG, "attending field exists");
}
}
}
}
From the docs:
public boolean exists ()
Returns true if the document existed in this snapshot.
Upvotes: 5
Reputation: 317402
What you're doing now is correct - you have to read the document and examine the snapshot to see if the field exists. There is no shorter way to do this.
Upvotes: 4