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 Using SQL ORMs with Node.js Defining Models Handle Validation Errors

Confused about the mapping method being used here

const errors = error.errors.map(err => err.message)

In the code above, can someone please explain to me what part .errors plays in this line?

is .errors a property of the error object?

Why wouldn't the line be const errors = error.map(err => err.message) instead?

2 Answers

I will start by saying that I don't really know all that much about error. Having said that, I can also say that I know enough about JavaScript that I can probably still answer your question. Yes, errors is a property of error. The fact that you are using a .map() method on it means that it is an array. You can't do const errors = error.map(err => err.message) because error is an object, and .map() only works on arrays.

Thanks Jason. I get it now.

Just to add to Jason's point I've added this from the lesson:

The error thrown by Sequelize contains an errors property, which is an array with 1 or more ValidationErrorItems, each represents a failed validation. Before displaying the error, we want to check the type of error.