Read Records question

A student of mine has created records in the data table, some of the properties hold lists of numbers. When he uses readRecords to get a copy of the data, the lists come back as lists of nulls instead of lists of numbers.
Any questions on how/why this is happening? I know objects and references to objects can act unexpectedly, but I’m not sure whats going on under the hood on this one.


@fleisch1984 Could you share a copy of the student’s code?

I believe the culprit is in the saveGame and/or loadGame sections, which are in the range line 345 - 400 or so.

I don’t expect that anyone will have time to comb through this program, but essentially what happens is that when you play and then use the game’s save feature, it writes game data to the records. One of the columns is completedStars which is a list like [12, 35, 123]. When the game is loaded, the code attempts to read the table to get back to the game state. The list of completed star numbers comes back as a list like this [null, null, null]. I can look in the table and verify that in the table, the list does contain numbers. They turn into nulls when read from the table.

I wasn’t able to understand why in the time I have looked at it, but I do know that reference types can act in unexpected ways. At least for novice programmers like myself!

Without looking at the code I’m guessing the problem is related to App Lab “stringifying” (or not) the array that you’re trying to save or load. Try experimenting with one or some combination of

JSON.stringify(list) on the way into the database
JSON.parse(records[i].completedStars) on the way back out.

I think I ran into this problem when I made this app:

Poke around in there to see what I did. Hope that helps,


1 Like