Hey there, everyone that they share.
And in this video, we're going to see that how we can update the user details now for updating the
user details.
We'll take help from the mongoose itself.
So in the mongoose there is a simple method which says Find by ID and update.
Now it is very easy for us to grab the ID.
We can always say request our user ID and grab that.
And for the update it takes a few parameters and options that you have to send to update the things.
So there are further down the road.
Notice here there are some examples that a find by ID and update and that you have to pass the ID through
which it's going to search the particular particular user.
In our case, then we have to send what is the data that you want to update.
So all the fields needs to be mentioned up there, then you can pass the option and optionally you can
get the callback as well to execute or not.
Something like that.
In the options you can pass on the values like new you can set this flag to true because by default
it is actually false.
And what it says is it returns the modified document.
So maybe your UI is synchronized in such a way that as soon as you update, you send this response and
that response goes in the display.
So however you want to accomplish that and then we have more fields like run validator, so it will
run all the validators like your email is in the correct format.
Your password is more than this character or whatever you are updating in the case.
We are not updating password in our case, but all the validators will run through itself.
So obviously we are going to go ahead and use to update the user details.
So let's go ahead and take example and help of this one.
Find by ID an update.
Let's move back up here.
So again, just like all of this, we are going to go ahead and copy this.
And just like always, I'll shrink this one, get some space, and we're going to go ahead and call
this one and simply say update user details.
So seems like a decent name this time.
Let's go ahead and work on with that.
I'm going to go ahead and select all of this and we'll just remove this one since we are going to use
the cookie as a token.
So I think we don't need to update this cookie token this time because we are not changing password
or any sensitive information, but maybe you want to send it.
It's up to you.
We'll be sending just a response that yeah, it is true totally on you.
You might want to update the cookie token in this case.
So totally 100% valid.
So all it says is first we need to hold a reference of this user.
So we're going to go ahead and just like always, we are going to say user database is always somewhere
else.
So we're going to go ahead and use this one.
So this user had a simple method which says Find by ID and update.
Now this fine by ID.
First thing is we need to pass on the user itself.
So we're going to go ahead and say request user ID, I can extract the ID anywhere I like thanks to
the middleware.
Then we need some of the options that need to be updated.
So user obviously will send me some of the new data.
As of now, I'm not thinking about what new data.
So let's go ahead and say let's call this one as simply new data and let's keep it as an empty object
for now.
So we're going to go ahead and say, hey, whatever the new data actually comes in, you just go ahead
and update this new data.
Now, further down the road, I'll also update a few options in this method.
The first one is new, which we just study that I need to grab whatever the new updated user data is.
I also want to run the validator, so I'm going to go ahead and say Run Validator Validators, thanks,
and we're going to go ahead and turn on true.
Now optionally, you can also go ahead and pass on this flag which is used to find use.
Come on, suggest me.
No, it's not going to say use, find and modify.
Now, this flag is a little bit controversial.
I always turn it off as a as a false.
In some of the previous versions of MongoDB, maybe your database is not that much updated.
They were using this find and modify.
Now the find and modify is not being used that much.
We rather prefer find by ID and update.
So that's why optionally you can turn this flag off.
But I guess in the most latest version it is already off.
So again, just check it out again and let me know also, okay, once this is all done, this is all
kind of a backward compatibility that we are doing.
Once this is all done and updated, then it's up to you.
Maybe you want to send a cookie token, maybe you want to update that.
Maybe you want to just send a simple resident status and status is going to be 200 because everything
is okay.
We're going to go ahead and simply send a JSON and let's go ahead and say success as true.
Now, this is where the interesting thing comes in.
Sometimes your front end developer is going to say that, hey, I don't need just the success.
True, I probably need the entire user as well at that time.
You can just go ahead and update your back end and say, okay, now you get the full updated user.
Again, this is all about how you are having a talk with the front end developer or what's the requirement.
But the problem is right now that user might be updating three things that we are going to allow.
As of now, he can update his name, email, password.
We have already taken care so we will not take care here.
Or maybe he wants to update the photo as well.
Maybe you have more data, you might want to go ahead and do that.
So in this field, let's just say anyways, we expect that however, whatever the front end guy is doing,
he loads the existing data and user is allowed to change whatever the data is changing.
But also send me all the data at the time of Updation.
If you are not going to send me name or something, it's going to be really bad.
So in the form itself, while the time of editing just load all the data and send me all the data at
this time.
So here we are going to say, hey, request dot, body, dot name if the name is existing because we
are assuming everything is updated, if the values are same, that's how we are going to take care.
And we are also going to say about email.
So request dot, body, dot, email in case you have more fields, then go ahead and just add that.
Now the problem comes in is the image itself.
Now it would be really great if we would have images right up in in our folder structure, in our application
itself.
But we don't have, we have the photos in the cloud.
Netty So what we've got to do first we have to check whether the file is coming in so photo is coming
in or not.
So we're going to go ahead and put up a check.
So we're going to say that, hey, if the request dot files dot photo is not equals to empty or you
can just check for if the request for point file is present or not.
So if this is present, like in this case, if it is not empty, that means you are sending me something.
And in that case I need to delete the previous photo and I need to upload whatever you are sending to
me.
So that's interesting.
So first and foremost, let's go ahead and find the user itself.
So we're going to go ahead and take help of user dot find by ID and we're going to go ahead and say
request dot, user dot ID, let's hold this reference into a variable.
So we're going to go ahead and say user and of course await.
There we go.
Now I have the ID of this user.
Through this ID, I would like to access the photo.
Let's go ahead and take a look on the model because it's a little bit confusing here.
So I have now this ID, so I have get all this information now inside the photo I would like to access
the ID so so the request, whatever the user is, user photo ID.
So that's the next one.
Okay.
So what this will give me, this will give me the idea of the image.
So let's go ahead and say, hey, I want to go ahead and say user dot photo dot ID.
So this will give me the ID of that particular photo.
Let's store that into available.
Also, let's call this one as image ID.
There we go.
Once I have this now based on this ID, I can delete the photos in the cloud.
So let's go ahead and do that.
So although I don't need to store that into a variable, but maybe you want to store that for some reason.
So let's go ahead and say, hey, this is going to be my response and all I have to do is await, of
course, because cloud data is also on another continent.
Let's go ahead and say this one.
So again, take health from cloud and we have already imported that we should have done the V two there
itself.
But anyways and again the same uploader method has a method that says destroy and all it takes is this
image ID.
We don't need to pass on the path itself.
It can find the path automatically and we can delete that.
Now this photo is deleted, so let's upload the photo that user has passed on to me.
So all I'm going to do is let's go ahead and copy some data because deleting part was important.
Uploading is not that much because we have already done that in the sign up part.
So let's go ahead and scroll that.
So this is we have the request files dot photo.
So this is all what we need.
So let's go ahead and copy this and go back up here.
And here we are updating the photo.
Don't worry, I'll write the comments for you.
So.
Or in fact, I can write it up here.
So this one is delete photo on cloud nary and this one is upload the new photo.
So this is uploading the new photo and we are providing the all parameters just like the last one.
So we are holding that into the result.
So result will again give me the two things, the ID and the security URL itself.
So what we're going to go ahead and do this.
We are going to update this object new data.
So let's go ahead and add a new property onto this new data.
So we're going to go ahead and say, hey, new data, you will have a new property photo and this new
photo is going to look like this.
It will have a property of ID.
Now this ID is going to be fulfilled by whatever the result I'm getting.
So result dot and this one is I guess public ID.
Yep.
Thanks.
Suggestion.
I love them and we need to hold the security url so security url is going to come in from result dot
secure url.
Let me verify that one more time again in the model.
So inside the model we have ID and we have secure URL type string.
Absolutely.
Fantastic.
So now if we have the photo inside is coming in or traveling in, then only we are going to update that.
Otherwise we will not.
Okay.
So looks great.
And anyways, we are storing that and this looks great.
So again, depends on how you are performing that.
Totally up to you.
What are the fields that you are changing or sending it to me?
So let's go ahead and create a root for this one.
So update details, this will go into roots.
Let's go ahead and duplicate this one.
And first, let's bring it up here.
There we go.
Save that.
Save that and what is the root?
That's interesting question.
Let's go ahead and say that slash user dashboard slash update.
I would love to copy this entirely.
Copy this.
Go back up here and this will be pasted up here.
Okay.
So now I have a new root, which is slash user dashboard slash update where I am allowed to update the
fields, whatever I'm sending in.
Let's go ahead and test it out.
Okay.
So let's go ahead and save this one.
And this one is going to be user details.
Update, update.
Okay.
So this report is going to go ahead and change like this, slash user dashboard slash update since we
are still logged in because we have the cookies and everything.
So it should be able to extract all these things.
So let's go ahead and send the name and the email and that's it.
We will not send the photo because that is optionally being checked.
So we're going to go ahead and say, Hey, name.
So the name of the user is two.
So let's go ahead and update that as to AE and please don't send the empty email.
Otherwise you will find nothing inside the body and it will just updated with the empty one.
Really don't want to do that.
Probably we want to put a check that there should be something inside the email and the body.
So we should be doing that and the email is going to be same to at the rate ae dot dev.
So again, all these data needs to travel.
Let's go ahead and send this one and it says cannot read the property.
A photo.
So this needs to have a photo as well.
Okay.
Let's go ahead and check it out.
Okay.
So this one says cannot read the property photo of and defines a photo we are not sending in.
So let's go into the middle.
Where it's not middle, it's the controllers.
And we need to check if the request files are coming in first.
So if the requested files are there, then only we can actually extract the request by bat.
This is a file.
This shouldn't be file.
This should be over a photo.
Are we calling it as photo?
Previously we are storing that into a into a variable.
That's why the problem is so it should be this time request dot files, dot photo, photo, dot temp
path.
And again we are checking this.
If the request files dot photo exist then only then that step path.
So it should be good.
Now let's try it one more time.
So this time we are sending name and email.
Let's go ahead and send it again.
And we are still receiving the error that I cannot read.
The property of the photo cannot read photo of undefined because we are not sending request files.
So we can actually go ahead and say if the request files exist, then we'll try to run all of this and
we'll try to extract this request, dot files, dot photo.
Then only if the files exist they should be coming up in the photo itself.
We can actually strengthen our code a little bit more, but again, we'll try it one more time.
Let's go ahead and send this one.
As the raw data at this time.
We got the success through.
Again, you get the idea.
The most important thing is you should be able to understand why the error is coming in and what more
you can do to handle that error.
But again, we need to check it up into the DB as well.
Let's go ahead and hit a refresh on this one and now we got the name being updated as to space.
AE Fantastic.
So this is definitely a much more strengthened code.
Really a mouthful there, but there we go.
Now again, we can go ahead and put up a simple check.
I'll just add it to do for you here that add a check for email and name in the body.
So go ahead and make a check.
If these are not there in the body, then go ahead and generate a custom error and simply say that hey,
email and an email and name should be compulsory, should be available, something like that.
So quite a fun, quite interesting one.
But all of our user controller is half done.
Yes, there is a little bit more on to that.
Let's go ahead and catch up in the next video.