Let's use KNEX to finish up some of our other routes. If we go to profile slash ID.
We don't really use this yet on our app.
It's something for future development.
This is a good example because sometimes backend engineers develop endpoints that might not be used
for the front end but they might need in future installations such as this one, where maybe we might
have a profile page for each one of our users and they can edit this and maybe they can have a post, a
delete, a put.
So let's work on this with profile dot ID, which want I grab the user.
That's a very simple operation with our database right.
We can simply remove this now and say DB dot select
everything because we want everything from the users table from the users table. Seems a little too easy
right.
Let's see if that works I'll do a dot then.
I'll return the user.
And just console log again just to test everything out we'll console log user.
Let's go to profile.
Slash ID.
And remember if we go back to our database we have ID 1, 2, 3, 6 so let's just do ID 1. Using our postman
we will grab the profile. We'll see if we have one of these here.
There you go.
There's a get request, and we'll do profile 1, send.
And if we go back to the console, all right we're getting all our users. But remember with profile ID we
want to make sure that it is the ID and not every single user that we're grabbing.
So we can simply say dot where and within the WHERE clause if we go back to KNEX and I want to demonstrate
to you here that this is what a developer does.
They don't know every single spec of a library or a package.
You simply get really good at reading documentation and seeing why you need such as here, and we
pass in an object of what we want to check against. So first name Test last name User.
In our case we want a object where ID is the ID that we received in the params. If we save and run this
again through postgres, go back to our example and look at that.
We have Ann. And we want to grab the array of the user, so we can just do - oops, and we can just do zero.
Amazing.
Just to keep this short
again, with ES6 we can do just this because both the id the property and the value are the same.
I'm going to save, and now, instead of console logging, we'll do response dot Json user, that will be the user
that we get.
And we don't need that found anymore, do we.
Let's clean up the code now.
And if this is a not found well, in that case we will get an empty array.
But let's just see what we get with our response first and then we'll touch the error case. We'll save
go back to postman, we'll send, and we get an error.
Found is not defined and we should comment this out.
We'll try that again click send.
All right we get Ann.
This is so cool.
And if we do 2
we get Anne with an e. If we do six we have Amy and if I do three I get John. What if I do a number that
doesn't exist.
I get 200 OK with no response.
Because well, we don't have that user. But let's just console log and see if we get back a user from this.
If I save and send I get an empty array.
So it's still returning something, it's just telling me that there's no user.
If I do a dot cache here - let me just fix the alignment here.
And I do the error with Json or response that status 400 and we'll just do not found if I save this
and go back to postman click send again with the wrong ID.
I still get 200 OK not the error that we catch because in javascript if I open up the chrome again. Trick
of javascript. If I do
Boolean array an empty array.
It's technically true.
So the way we want to check against the empty array we simply say if user dot length exists in that
case it will be greater than 1 or equal to 1 will respond with the user.
Otherwise we'll respond with
our status not found and maybe we can also in here say
error getting user. If we save now and go back to - let's remove console because we don't need it anymore.
Go back to postman.
Click send. Not found.
That's awesome.
We got a 400 bad request but if we grab somebody that we want it's now working.
Very cool. We can now remove this ugly code.
We have much better syntax here now.
And we also have the register done.
We'll leave the sign in till the end.
But let's work on the image endpoint in the next one.
I'll see on that one. Bye-bye.