QuickTip: Store Undefined and Null in Class/Table Rows on Parse.com

Update: Parse is shutting down. So you should start migrating.

Parse store undefined and null

I’ve been working on an Android app backed by Parse.com to make my development process faster and easier. So while coding the app I made a decision to have 2 classes (tables) called Conversations and User (exists by default). Conversations has a field called userId that should essentially store the objectId from User class. But some conversations are created by guest so for those records I decided to store NULL or “nothing” (undefined) under the userId column rather than Pointers which is what the column type is.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

Storing undefined

So when a field doesn’t exist for a record (can be related to mongodb where documents [rows] in collections [table] can have variable fields rather than a strict schema), Parse shows (undefined) (in a dull grey color) as its value. Hence, the first thought that came to my mind to “not store” any data for userId when the creator is anonymous is to just not put() any data for it. But the problem with this method is that if the class does not exists and there’s an attempt to save a record or document (thinking mongodb) to it without assinging any value to userId, then in the Data Browser of Parse the column just won’t show up. Any subsequent records with a Pointer to the User class will not affect this in any way, i.e., it won’t show up the userId column – atleast that’s what happened to me.

The solution to this problem is really simple but might be hard or time-consuming to figure out. All that you need to do is, click the “+ Col” button to add a column in the user interface table and give it the apt name which is userId in our case along with the proper data type which is Pointer in our case. As soon as the column is created, it’ll simply appear (sort of unhide itself) with all the undefined and Pointer data for various rows in it.

Storing NULL

If you try to store null in Android (Java) it’ll give you an error saying java.lang.IllegalArgumentException: value may not be null. To circumvent this, we’ll have to pass JSONObject.NULL to the put() method while saving a value against that field as mentioned in the API Reference.

In this case too, if the first record/document has JSONObject.NULL as the userId then the column won’t show up (in the user interface of Data Browser) even when a Pointer (or some other data type like String, Int, Date, Array, Object, etc.) is stored later. So to remedy this situation we’ll have to follow the same step as described earlier which is to add a column with appropriate name and data type.

While working with JavaScript Parse SDK, storing undefined or null wasn’t a pain at all. If you want to store NULL then just pass null as the value. For undefined either assign the same or don’t assign anything to the field/key.

Hope that helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Author: Rishabh

Rishabh is a full stack web and mobile developer from India. Follow me on Twitter.

Leave a Reply

Your email address will not be published. Required fields are marked *