How to properly read array from a data table on Code.Org AppLab?

951 Views Asked by At

I created a table called "morning" in AppLab, and one column stores data as an array (or list as it calls it). I'm able to properly add data to this array, but my problem is reading the data back (as I want to display it as a label/normal text on another page) If the numbers 1234 and 5678 are the values in the array, when I try to do

console.log(records[i].id + ': ' + records[i].buses);    

The second value (buses) is the name of the column I'm trying to read back, which will result in "," rather than "1234,5678" and I'm not really sure what to do. This is the code I have so far, any help would be greatly appreciated!

readRecords("morning", {}, function(records) {
  for (var i =0; i < records.length; i++) {
console.log((records[i]).id + ': ' + records[i].(buses[i]));
  }
 });

 var ts1Buses = ["1234"];
 var ts1Change;
onEvent("enterTS1", "click", function(event) {
  appendItem(ts1Buses, getText("textTS1"));
  updateRecord("morning", {id:1, buses:ts1Buses}, function(record, success) {
    setText("textTS1", "");
  });
});     
2

There are 2 best solutions below

0
On BEST ANSWER

The console.log statement in your longer block of code doesn't look quite right. try console.log(records[i].id + ': ' + records[i].buses); instead. if that doesn't work, please post a link to your project so that others can try to find a fix by remixing and editing it.

0
On

App Lab's data tables do not support arrays. They will have to be converted into comma-separated strings before creating or updating and converted to an array after reading.

To convert an array to a string, simply use the toString() method:

var array = ["a", "b", "c"];
console.log(array.toString()) // "a,b,c"

To convert a string into an array, use the split() method:

var string = "a,b,c";
console.log(string.split(","); // ["a", "b", "c"]