Waseem Ahmed
Waseem Ahmed

Reputation: 419

Is it possible to do a nullpoint check on a field between documents in Firestore

In Firestore, do all document have to be identical in respect of their fields?

Example:

If document A has two fields, field 1 and field 2 and document B only has field 1, when I run a query related to field 2, then is there a way to put in a nullpointer check because I know that docment B would give me an error.

I know that documents are never null, but I was wondering if there was a way of putting in a safety measure on the field level?

Note: to Doug and Alex as my response is too long:

Thanks Alex and Doug, I remember viewing the video which indicated that Firestore is free-form and hence I constructed the database to be such i.e. allow me to expand as my app develops.

I understand your example query above with the whereEqualTo of null but if i had a query as follows:

`queryForNullvalues = rootref.collection("coll).whereEqualTo("Field1","some random value")`, 

Then when I get down to the next level and I have a

val fieldvalue = value.getString("field2") , 

Then what you will find is that the app crashes because if I was doing this query on document 2, field 2 does not exist. This is inherently where the issue lies. Is the app meant to crash on that? because the logcat would give an error of nulpointer. Just to be clear if i were to use

val fiedlvalue = value.getString("field1") 

It doesn't crash as field1 exists in both documents

Upvotes: 0

Views: 52

Answers (2)

Alex Mamo
Alex Mamo

Reputation: 138969

While using security rules, as @DougStevenson mentioned in his answer, it's a solution to solving this problem, please also note that there is another approach that you might be interested in.

According to the official documentation, Firestore documents can contain properties that can hold null values. So null is a supported data type:

Cloud Firestore lets you write a variety of data types inside a document, including strings, booleans, numbers, dates, null, and nested arrays and objects.

So if you query a collection for documents and some of them have properties with null values, when getting the values of such properties, a NullPointerException will be thrown. This approach is helpful because it can also allow you to create queries that can limit the results where a property holds the value of null. In Kotlin, such a query looks like this:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance()
Query queryForNullValues = rootRef.collection("coll").whereEqualTo("field2", null)

One more thing that you should care about is that this solution is not the same as missing a property. If you are using model (POJO) classes to write data to Firestore, all empty properties will automatically be saved as null rather than not at all.

Edit:

As I already mentioned in my answer, when you are using the following line of code:

val fieldvalue = value.getString("field2")

You can get a NullPointerException if some documents in your collection hold null as a value in field2.

This is inherently where the issue lies.

Isn't this what you were looking for "is there a way to put in a nullpointer check because I know that docment B would give me an error."?

To solve that, simply check your fieldvalue against null:

if(fieldvalue != null) {
    //Use the value of fieldvalue
}

Is the app meant to crash on that?

Yes and this is because you are trying to get a value that is null.

val fiedlvalue = value.getString("field1")

It doesn't crash because there is no document in your collection that hold in your field1 property the value of null.

Upvotes: 4

Doug Stevenson
Doug Stevenson

Reputation: 317702

Firestore imposes absolutely no requirements on the fields in a document. It's completely free-form. As such, it's said to be a "schema-less" database.

The only way to enforce any requirement that fields exist and contain correct data is via security rules. This only applies to web and mobile clients. Backend code always bypasses security rules.

If you can't ensure that all code that writes documents conforms to some requirements, then all the code that also queries documents should check each field to make sure it exists, is the correct type, and so on.

Upvotes: 0

Related Questions