Changing this if condition from this:

if (props.items.length === 0) {

To

if (!props.items?.length) {

Should work.

Similarly if using for places,you can check using ternary operator if length exists in array of places.

How? Because items could possibly be null or undefined from apis, so the length property on array might be missing.

Moreover, using ! Not operator would make this condition true if length of items is 0 or is null or undefined or any falsy value

Answer from Murtaza Hussain on Stack Overflow
Discussions

angular - Jasmine unit tests - TypeError: Cannot read properties of undefined (reading 'pipe') - Stack Overflow
TypeError: Cannot read properties of undefined (reading 'pipe') at MyComponent.fetchData I'm getting the above error when running my Jasmine unit tests in Angular. Existing beforeEach's don't like ... More on stackoverflow.com
🌐 stackoverflow.com
April 24, 2022
typeerror - JavaScript - Cannot read properties of undefined (reading 'length') - (Code does work and provided) - Stack Overflow
I have looked at many different methods here on SO, and neither of them will resolve the issue I am facing. If anything, they all still show the same Error. The count starts when you type a number ... More on stackoverflow.com
🌐 stackoverflow.com
TypeError: Cannot read property 'length' of undefined, when karma tests re-load
This bug report is from using Angular2 and Karma/Jasmine But the error is emitted from Typescript. I am hoping that is the relevant part. Versions _ _ ____ _ ___ / \ _ __ __ _ _ _| | __ _ _ __ / __... More on github.com
🌐 github.com
19
February 9, 2018
angular - Karma-Jasmine: TypeError: Cannot read properties of undefined (reading 'get') - Stack Overflow
I am trying to write a unit test for my angular component and I am stuck at this error. I have researched a lot about this on stack overflow and some online docs. Here is the link of resources whic... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Rollbar
rollbar.com › home › how to fix typeerror: cannot read property length of undefined in javascript
How to Fix TypeError: Cannot Read Property Length of Undefined in JavaScript
Verify that nested properties exist ... TypeError: Cannot read property 'length' of undefined happens when you try to access the length property on something that doesn't exist....
Published: July 13, 2025
🌐
Stack Overflow
stackoverflow.com › questions › 78264410 › javascript-cannot-read-properties-of-undefined-reading-length-code-does
typeerror - JavaScript - Cannot read properties of undefined (reading 'length') - (Code does work and provided) - Stack Overflow
Typing in 100.1 will show 1 However, when you first click on the field, you will get the Uncaught TypeError. Cannot read properties of undefined (reading 'length') The code is originally written with a hardcoded value.
🌐
Career Karma
careerkarma.com › blog › javascript › typeerror: cannot read property ‘length’ of undefined
Typeerror: Cannot Read Property ‘length’ of Undefined | Career Karma
January 4, 2021 - function readingErrorsAreImportant(a){ if(undefined !== a && a.length){ return "if undefined does not equal a and the length of a, I will be returned." } else { return "else, or if it is equal to undefined, I will be returned.
🌐
GitHub
github.com › angular › angular-cli › issues › 9588
TypeError: Cannot read property 'length' of undefined, when karma tests re-load · Issue #9588 · angular/angular-cli
February 9, 2018 - It looks to me, like the problem is that sourceText, is undefined. And we're calling length on it here. If I reset the sourceText to an empty string, when it is undefined or null. My tests re-run on changes, with no errors.
Author: angular
Find elsewhere
🌐
Reddit
reddit.com › r/learnjavascript › [deleted by user]
[deleted by user] : r/learnjavascript
July 15, 2022 - You’re getting this error when calling the length property on something. That means that something you have use .length on is undefined at some point.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-cannot-read-property-length-of-undefined
Cannot read properties of undefined (reading 'length') in JS | bobbyhadz
The error commonly occurs when you access an array at an index that doesn't exist and get an undefined value back. ... Copied!const arr = ['bobby', 'hadz', 'com']; // ⛔️ TypeError: Cannot read properties of undefined (reading 'length') const ...
🌐
Rollbar
rollbar.com › home › how to fix typeerror: cannot read property of undefined in javascript
How to Fix TypeError: Cannot Read Property of Undefined in JavaScript
Cannot read properties of undefined (reading 'length', 'data', any key) means the object is undefined. Fix it with checks, optional chaining, defaults.
Published: 2 weeks ago
🌐
Pinecone Community
community.pinecone.io › support
ES Importing PineconeClient returns [TypeError: Cannot read properties of undefined (reading 'match')] running Jasmine tests - Support - Pinecone Community
April 4, 2023 - On my Node.js app I have installed @pinecone-database/pinecone@0.0.10 along with langchain, whenever I have PineconeClient imported, Jasmine tests fail reporting: [TypeError: Cannot read properties of undefined (reading 'match')]. When I comment out the import statement my tests just run fine.
🌐
TrackJS
trackjs.com › javascript answers › how to fix `cannot read properties of undefined (reading 'length')`
How to fix `Cannot read properties of undefined (reading 'length')` • TrackJS
April 8, 2026 - Common JavaScript error when accessing length property on undefined values. Usually occurs with arrays, strings, or API responses that haven't loaded yet. Quick fixes: add null checks, use optional chaining, handle loading states.
🌐
Reddit
reddit.com › r/learnprogramming › typeerror: cannot read properties of undefined (reading 'length')
r/learnprogramming on Reddit: TypeError: Cannot read properties of undefined (reading 'length')
May 22, 2022 -

I am getting a type error in my code as given in the title:

TypeError: Cannot read properties of undefined (reading 'length')

I have researched the error message and can't figure out what's going on.

Here is the full code, specific block throwing the error is given below.

// All valid credit card numbers
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8]
const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9]
const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6]
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5]
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6]

// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5]
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3]
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4]
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5]
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4]

// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4]
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9]
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3]
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3]
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3]

// An array of all the arrays above
const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5]


// Add your functions below:

const validateCred = (arr) => {
    let newArr = [];
     for (let i = arr.length - 1; i >= 0; i--){
         let sec = arr[i];
        if (((arr.length - 1) - i) % 2 === 1) {
            let num = arr[i] * 2;
            if (num > 9) {
               num -= 9;
            }
            newArr.push(num);
        } else {
            newArr.push(sec);
        }
     }
        let sum = newArr.reduce((partialSum, a) => partialSum + a, 0)
         if (sum % 10 === 0) {
             return 'Valid';
         } else {
             return 'Invalid';
         }
}

const findInvalidCards = (arr) => {
    let invArr = []
    for (let i = 0; i < arr.length; i++) {
        if (validateCred(arr[i]) === 'Invalid') {
            invArr.push(arr[i])
        }
    }
    return invArr;
}

let invArray = findInvalidCards();

console.log(invArray);

const cardFunc = (list) => {     
    let compArr = []; 
        switch (list) {
            case (list[0] === 3):
                compArr.push('Amex');
            case (list[0] === 4):
                compArr.push('Visa');
            case (list[0] === 5):
                compArr.push('Mastercard');
            case (list[0] === 6):
                compArr.push('Discover');
}
    let filterList = (arr) => { 
        return arr.filter((index, item) => arr.indexOf(item) === index);
    }

    return filterList;
}

This is the block that is throwing the error.

const findInvalidCards = (arr) => {
    let invArr = []
    for (let i = 0; i < arr.length; i++) {
        if (validateCred(arr[i]) === 'Invalid') {
            invArr.push(arr[i])
        }
    }
    return invArr;
}

Any ideas? Thanks in advance!