I am using reduce() in one of my functions to get a single value from some documents. Generally, this works as expected. However, once in a while I get an error "reduce is not a function". I know that, according to the docs, "Calling reduce() on an empty array without an initial value is an error" -- so I am passing "0" to handle that. But I still sometimes - though rarely - get the error. Just trying to understand what's going on.
My code looks like this:
let totalOpenBalance = docs.reduce((a, curr) => a + curr.openBalance, 0);
The data I'm calling this on looks like this:
[
{
"_id": "3s14020471287f4a9bbe9878",
"openBalance": 120,
"field3": 25
},
{
"_id": "2d75020471986f4a9bbe7231",
"openBalance": 15,
"field3": 22
}
]