The following forEach throws: TypeError: undefined is not an object (evaluating ‘results.items.forEach’).
The query returns 5 items.
Note: incAmount is the field key in the collection that I am trying to sum.
Can someone please correct this code?
For test purposes, I rearranged the code to load the table with the results from the query before the forEach loop. It clearly show 5 items in the table with a value in the Inc Amount (table field) for each item.
results.length also returns 5.
However, the newly added console.log(results.items) throws an undefined error. So, since I know results returned 5 valid items, not sure why the console.log throws the undefined error.
Here is the modified code:
getTaxData(yearChoice)
.then ( (results) => {
console.log(“numItems: " + results.length); // 5
console.log(results.items); //undefined
$w(”#taxTable").rows = results; // loads 5 items into table - incAmount has a value in each item
let sumTotal = 0;
if (results.length > 0) {
results.items.forEach((item ) => { // TypeError: undefined is not an object (evaluating ‘results.items.forEach’)
sumTotal = sumTotal + item.incAmount; // add each value to sumTotal
});
$w(“#totalAmount”).value = sumTotal;
}
})
. catch ((err) => {
console.log("dropYear_change error: " +err);
})
Here is a screen shot of the table showing the 5 items. Note, the number values in Inc Amount column.
I changed the code to the following replacing the forEach with the reduce function. It works, but returns an object as you can see in the field below the table in the screenshot.
getTaxData(yearChoice)
.then ( (results) => {
console.log(“numItems: " + results.length); // 5
$w(”#taxTable").rows = results; // loads 5 items into table - incAmount has a value in each item
It sure looks like your latest code suggestion should work, but it throws and error:
TypeError: undefined is not an object (evaluating ‘results.items.reduce’)
Note: the incAmount field in the collection is of type Number.
Here is my code:
getTaxData(yearChoice)
.then ( (results) => {
console.log(“numItems: " + results.length); // 5
$w(”#taxTable").rows = results; // loads 5 items into table - incAmount has a value in each item
It depends on the return of getTaxData() which you haven’t posted.
My answer is true for a standard unprocessed collection query.
(Your getTaxData() probably processes the query results and returns results.items).
Anyway I’m glad you resolved the problem.