juicya
juicya

Reputation: 3

Uncaught TypeError despite undefined check

I am getting this error.

I am trying to filter by time of day, and I'm receiving the inputs from two HTML time fields.

export function timeBetweenFilterFn(rows, id, filterValues) {
const startVals = filterValues[0] ? filterValues[0].split(':') : undefined;
const endVals = filterValues[1] ? filterValues[1].split(':') : undefined;


const [startHour, startMinutes] = startVals ? 
        startVals.map( (val) => {
            return parseInt(val);
        })
        : undefined;
const [endHour, endMinutes] = endVals ? 
        endVals.map( (val) => {
            return parseInt(val);
        })
        : undefined;

if (startVals || endVals) {
    return rows.filter( (row) => {
        const [cellHour, cellMinutes] = row.values[id].split(':').map( (val) => {
            return parseInt(val);
        })
        if (cellHour === startHour){
            return cellMinutes >= startMinutes;
        }
        else if(cellHour === endHour){
            return cellMinutes <= endMinutes;
        }
        else{
            return cellHour >= startHour && cellHour <= endHour;
        }

    })
}
return rows;

}

when either the start field or end field is not given, I get the uncaught type error when I try to map the array to the new values despite the ternary check.

Upvotes: 0

Views: 213

Answers (1)

Doc
Doc

Reputation: 681

If you use undefined as the fallback value, you will get an error cause you are trying to get variables [startHour, startMinutes] and [endHour, endMinutes] from undefined, which is not iterable.


You can't try to get variables from undefined.

As you can see in this example made in the browser console, i get the same error, because undefined is not iterable.

enter image description here

Instead of undefined, try using [] as default value in order to avoid errors.

enter image description here

In this case the variables a and b will simply be undefined.

Upvotes: 1

Related Questions