Problem with forEach loop

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?

getTaxData(yearChoice)
.then ( (results) => {
console.log(“numItems: " + results.length); // numItems: 5
let sumTotal = 0;
if (results.length > 0) {
results.items.forEach((item ) => {
sumTotal = sumTotal + item.incAmount; // add each value to sumTotal
});
$w(”#taxTable").rows = results;
$w(“#totalAmount”).value = sumTotal;
}
})
. catch ((err) => {
console.log("dropYear_change error: " +err);
})

console.log(results.items); and see if it’s ordered in the array as expected and if each element in the array has a value of incAmount .

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.

Instead of forEach(), it makes more sense to use reduce()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce#Sum_of_values_in_an_object_array

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

            $w("#totalAmount").value = results.reduce((accumulator, currentValue, currentIndex, array) => { 
                  **return**  accumulator + currentValue; 
            }) 

If I define a variable called currentValue and initialize it to 0 before the results.reduce function, the result is the same.
let currentValue = 0;

//code.....
let initialValue = 0;
let sum =  results.items.reduce(
    (accumulator, currentValue) => accumulator + currentValue.incAmount
    , initialValue
);
$w("#totalAmount").value = sum;
//code...

Thanks for hanging in there with me J.D.

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

            **let**  initialValue = 0; 
            **let**  sum = results.items.reduce( 
                (accumulator, currentValue) => accumulator + currentValue.incAmount, initialValue); 
            $w("#totalAmount").value = sum; 
        }) 
        . **catch**  ((err) => { 
            console.log("dropYear_change error: " +err); 
        })

SOLVED!!!
I changed results.items.reduce TO results.reduce AND IT NOW WORKS!! Reduce was looking for the array not the items in the array.

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.

Thank you for helping me solve this problem. I really appreciate it!!!