Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Using SQL and Node.js with Sequelize!
You have completed Using SQL and Node.js with Sequelize!
Preview
In this video we'll validate user input and handle any uncaught errors.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
All of our database code assumes
that everything is going to be fine.
0:00
But remember,
0:05
promises also have a catch method to
handle a promise if it's rejected.
0:06
Let's catch every database call, and
then send a 500 error to the user.
0:24
This is using Express' send method.
1:00
This specific error will be shown in the
browser in the development environment,
1:03
but will be logged in production.
1:08
How about if the article is missing?
1:11
This could happen when we run
the findById with an invalid id.
1:14
We can send a 404 to the client to let
them know that the resource can't be
1:22
found.
1:27
So, if the article is present,
we can do what we normally do.
1:45
Else, we send the 404 and
we can do this for the edit form.
1:50
The delete form.
2:02
The individual article page.
2:18
The PUT, or update path.
2:49
And finally, the deletes or destroy path.
3:05
Nice, so when we restart the server,
3:21
And go to a URL of an article
that doesn't exist, we get a 404.
3:31
Next, we want to prevent invalid data
to be entered into the database.
3:43
For example,
we want all articles to have titles.
3:48
Our rooms can automatically
validate models.
4:06
You can specify it validates it on
an attribute, as well as a data type.
4:08
Here's a list of all the validations
supported by Sequelize.
4:29
We're wanting to prevent empty strings, so
the notEmpty validators should be used.
4:32
If we scroll down further, there is an
example of using a custom error message to
5:09
appear when validation fails.
5:14
When we try and save an article or
update it without the title,
5:41
an error is patched
into the catch methods.
5:45
Here, And
5:49
here, we don't want the server
to respond with 500 errors,
5:56
which means that you
would get a server error.
6:00
Instead, we want to re-render the new and
edit forms to display the error messages,
6:04
so that the user can
correctly fill out the form.
6:10
So before each of these catches,
we want to test the type of the error.
6:18
If the error is of the type
6:32
SequelizeValidationError.
6:38
We want to re-render the forms.
6:49
If not, We
6:51
wanted to throw the error,
to be handled by the final catch.
6:56
Finally, we want to render the form and
the errors for creating and editing.
7:33
Let's copy the new form from the render
again, but instead of the article
7:47
without any properties set,
we can use the request body.
7:51
On the error, there's an errors array and
8:13
we can pass that into
the view to be rendered.
8:16
The error code is there already
that iterates over the errors and
8:20
displays them.
8:23
Then we want to do the same for
the edit page.
8:30
But this time when we build an article,
we need to give it the correct id,
9:13
since the id is in the URL as a parameter,
and not in the form body.
9:18
This will make sure that
the correct article gets updated.
9:25
When we restart the server and attempt
to create the post without the title,
9:31
it errors.
9:35
Actually it doesn't, because the key for
9:36
the errors should be errors, not error.
9:41
So let's restart this again.
9:46
And try it out.
9:52
There we have the errors.
9:55
When we attempt to reserve,
it saves without an error.
9:59
Let's try and remove the title in an edit,
save, and it errors again.
10:04
Nice.
10:10
And there we have all the code
operations using Sequelize, and
10:11
we now know how to handle errors too.
10:15
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up