Oct 29, 2021 08:40 AM
Hi, I am wondering how null
cell values are structured in query results.
For example, when I iterate over the query results from a selectRecordsAsync()
function, e.g. with a .forEach(map(x => {...}))
function, when I hit a null
value, the script errors. It’s easy enough to skip over these null values by wrapping the functionality in condiional e.g. if (x !== null) {...}
but how do the arrays containing the query results represent the null values?
Are the null values empty objects, or empty array indexes?
I tried to emulate the data structure in node:
> let m = [{}, {id: 1}, {id: 2}, {}, {}, {id: 3}] // <-- empty objects
> console.log(m.map(a => a.id))
[ undefined, 1, 2, undefined, undefined, 3 ]
> let p = [, {id: 1}, {id: 2}, , , {id: 3}] // <-- empty indexes
> console.log(p.map(b => b.id))
[ <1 empty item>, 1, 2, <2 empty items>, 3 ]
Is what node reports as <# empty item(s)>
a null value, i.e. is the query result returning an array of objects where there exists values, but empty array indexes where there are not? Or, am I misconceiving the data structure for field values?
The following is an example of the code used to produce these null errors:
// Load the Example table object
let table = base.getTable("Example Table");
console.log(table); // <-- the whole table as a single object of id, name, description, url, fields
console.log(Array.isArray(table)); // <-- false
// Query for all the record objects from the table
let query = await table.selectRecordsAsync({
sorts: [
{field: "Created"}
],
fields: [
"Linked Field" // <-- a linked field on the table which allows for multiple record links
]
});
console.log(query); // <-- "{recordIds: Array(#), records: Array(#)}" a single object containing two keys, each with a value which is an array of a) recordID objects {id: "strings"} and b) an array of record objects {id: "string", name: "string"}
console.log(Array.isArray(query)); // <-- false
// Grab the objects in the records array
let exampleTableRecords = query.records;
console.log(exampleTableRecords); // <-- "(#) [Object, Object, Object, ...]" an array of all of the table's record objects {id: "string", name: "string"}
console.log(Array.isArray(exampleTableRecords)); // <-- true
// Iterate over the query results for each record's "Linked Field" values
exampleTableRecords.forEach(q => {
let exampleFieldArray = q.getCellValue("Linked Field");
console.log(exampleFieldArray); // <-- null OR "(#) [Object, Object, Object, ...]"
console.log(JSON.stringify(exampleFieldArray)); // <-- "null" OR "[{"id":"rec...","name":"..."},{"id":"rec...","name":"..."}, ...]"
console.log(Array.isArray(exampleFieldArray)); // <-- false on null values OR true
console.log(exampleFieldArray.map(m => m.id)); // <-- this will error on null values OR "(#) [array of RecordIDs, ..., ...]"
// let fieldArrayIDs = exampleFieldArray.map(r => r.id);
})
Error msg:
TypeError: null is not an object (evaluating 'exampleFieldArray.map')
Solved! Go to Solution.
Oct 29, 2021 12:32 PM
null
is a data type in JavaScript. It is not an empty object, and it is not an empty array. It is null
. You cannot map or iterate null
.
When a cell is “blank”, its value is null
. For example, if you try to get the cell value of a linked record field with no linked records, you will get null
, not an empty array.
Oct 29, 2021 12:32 PM
null
is a data type in JavaScript. It is not an empty object, and it is not an empty array. It is null
. You cannot map or iterate null
.
When a cell is “blank”, its value is null
. For example, if you try to get the cell value of a linked record field with no linked records, you will get null
, not an empty array.
Oct 30, 2021 08:16 AM
ooooOOOOOOooooooh, okay - so rather than empty values or values that are empty objects, there is an explicit null
for empty cells?
e.g. iterating a map function over:
let q = [{id: 0}, {id: 1}, {id: 2}, null, null, {id: 3}];
…would exhibit the same data type error:
> console.log(q.map(c => c.id))
Uncaught TypeError: Cannot read property 'id' of null
at REPL34:1:26
at Array.map (<anonymous>)
Thank you!!