Let's look at what we just did in the previous video. We've made our database communicate with the server,
and we've registered our first user which was Ann.
But now we want to respond to the Front-End with this new user,
Ann. So how can we do that.
Well if this is success, in which case we want to do a dot then,which we have here.
If we get a response, in that case we want to
send a response to Json and this response dot json will respond with...
Well what should it respond with?
Luckily for us KNEX again is built with this in mind, and has a feature that is - or a method that
is called returning.
So instead of us doing another select statement and grabbing Ann we can just say returning all.
So this says that users insert Ann and return all the columns.
Let's see if that works.
If that works I can just place the response here.
Let's check it out.
Go into save. Open our postman.
I'm going to add a new user, we'll just say Anne with an e. Hit send, like that. I get an array that is Anne.
Amazing.
Looks like it's working.
If I go back to the database and do select star from users there's two users but the one that I've received
is just Anne. Let's create a new user. I'm going to create John, cookies.
We remember this guy. Click send. We get John.
Amazing.
Let's check with the front end to see that this actually works.
If I go back here - let's just change this into user just so it's more descriptive.
So we're returning the user but we also want to make sure that it's not an array and we're returning
the object.
So we'll just do it user.
The first one because when you register a user they should only be one.
And we also want to make sure that if, in case there's an error, we respond.
With a response dot Json error or perhaps you can even do a status 400. Let's give that a go - and this
should be
Sorry.
Catch.
Let's say that again - to catch any errors that may have happened here.
If I go back here and try to register with the same John I get - look at that - an error.
And we see that key e-mail John already exists.
This is an important point here.
We get a 400 error but we are returning information about our database.
We now know that in this database John already exists.
And one of the big security principles is that you want to make sure that you don't give the client
any information about your system.
And this is not good.
So perhaps instead of returning the error we can just say "unable to join" or "unable to register". If we
save, send again. Unable to register - 400 - bad request. Amazing.
Let's see if that actually works with our front end now.
I go back to my app click on register and let's register with a completely new user will do Amy
e-mail will be TheAmy@gmail.com password 1 2 3 - oops - password 1 2 3. Register. Amy your current entry
count is zero.
How cool is that.
And just to make sure that Amy got registered.
If we go back to our database and I do select or I can just press up because we've done this before
I have Amy in my database. So cool.
So we've done the register.
Everything looks pretty nice.
There's a few more endpoints to fix so let's do that.
I'll see you in the next one.
Bye-bye.