{"id":1739,"date":"2024-05-04T05:25:57","date_gmt":"2024-05-03T23:55:57","guid":{"rendered":"http:\/\/codetheory.in\/?p=1739---6ded4f79-bc0e-41f8-8ef2-60f5bae2a2b5"},"modified":"2024-05-04T05:25:57","modified_gmt":"2024-05-03T23:55:57","slug":"store-undefined-and-null-on-parse-com","status":"publish","type":"post","link":"https:\/\/codetheory.in\/store-undefined-and-null-on-parse-com\/","title":{"rendered":"QuickTip: Store Undefined and Null in Class\/Table Rows on Parse.com"},"content":{"rendered":"

Update:<\/strong> Parse is shutting down<\/a>. So you should start migrating.<\/p>\n

\"Parse<\/p>\n

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

<\/p>\n

Storing undefined<\/h2>\n

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)<\/code> (in a dull grey color) as its value. Hence, the first thought that came to my mind to “not store” any data for userId<\/code> when the creator is anonymous is to just not put()<\/code> 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<\/code>, then in the Data Browser of Parse the column just won’t show up. Any subsequent records with a Pointer to the User<\/code> class will not affect this in any way, i.e., it won’t show up the userId<\/code> column – atleast that’s what happened to me.<\/p>\n

The solution<\/strong> 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<\/code> in our case along with the proper data type which is Pointer<\/code> in our case. As soon as the column is created, it’ll simply appear (sort of unhide itself) with all the undefined<\/code> and Pointer<\/code> data for various rows in it.<\/p>\n

Storing NULL<\/h2>\n

In this case too, if the first record\/document has JSONObject.NULL<\/code> as the userId<\/code> then the column won’t show up (in the user interface of Data Browser) even when a Pointer<\/code> (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.<\/p>\n

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

Hope that helps!<\/p>\n","protected":false},"excerpt":{"rendered":"

Update: Parse is shutting down. So you should start migrating. 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 … Continue reading “QuickTip: Store Undefined and Null in Class\/Table Rows on Parse.com”<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[105,127,8,89,122],"_links":{"self":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/1739"}],"collection":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/comments?post=1739"}],"version-history":[{"count":5,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/1739\/revisions"}],"predecessor-version":[{"id":2289,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/1739\/revisions\/2289"}],"wp:attachment":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/media?parent=1739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/categories?post=1739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/tags?post=1739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}

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