Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

An issue with the Stats. TypeError: Cannot read property 'length' of undef

const totalPlayers = props.players.length;

I am getting the error TypeError: Cannot read property 'length' of undefined after the changes on the players.

Thanks

rydavim
rydavim
18,814 Points

It will be difficult to troubleshoot with only this short snippet. If you're able to post a workspace snapshot using the camera icon towards the top right, or post your code using markdown it will help community members help you!

It may also be helpful to link to or give the title of the course you're working from.

1 Answer

Simon Coates
Simon Coates
8,223 Points

you haven't posted your code, but I'll try and recreate the error.

let props = {
    players: []
}
let totalPlayers = props.players.length; //works
props = {
}
console.log("running1");
totalPlayers = props.players.length; //fails
console.log("running2");

produces output of:

VM192:9 running1
VM192:10 Uncaught TypeError: Cannot read property 'length' of undefined
    at <anonymous>:10:30

So you should probably verify if there's an array at props.player.