WEBVTT

00:03.820 --> 00:05.260
Hey there, everyone that they share.

00:05.260 --> 00:10.000
And in this video, we're going to see that how we can update the user details now for updating the

00:10.000 --> 00:10.690
user details.

00:10.690 --> 00:12.730
We'll take help from the mongoose itself.

00:12.730 --> 00:16.780
So in the mongoose there is a simple method which says Find by ID and update.

00:16.810 --> 00:18.820
Now it is very easy for us to grab the ID.

00:18.820 --> 00:21.790
We can always say request our user ID and grab that.

00:21.790 --> 00:27.490
And for the update it takes a few parameters and options that you have to send to update the things.

00:27.490 --> 00:29.440
So there are further down the road.

00:29.650 --> 00:34.300
Notice here there are some examples that a find by ID and update and that you have to pass the ID through

00:34.300 --> 00:38.050
which it's going to search the particular particular user.

00:38.050 --> 00:41.260
In our case, then we have to send what is the data that you want to update.

00:41.260 --> 00:45.580
So all the fields needs to be mentioned up there, then you can pass the option and optionally you can

00:45.580 --> 00:47.710
get the callback as well to execute or not.

00:47.710 --> 00:48.700
Something like that.

00:48.700 --> 00:53.980
In the options you can pass on the values like new you can set this flag to true because by default

00:53.980 --> 00:55.060
it is actually false.

00:55.060 --> 00:58.210
And what it says is it returns the modified document.

00:58.210 --> 01:03.580
So maybe your UI is synchronized in such a way that as soon as you update, you send this response and

01:03.580 --> 01:05.080
that response goes in the display.

01:05.080 --> 01:11.290
So however you want to accomplish that and then we have more fields like run validator, so it will

01:11.290 --> 01:15.610
run all the validators like your email is in the correct format.

01:15.610 --> 01:19.090
Your password is more than this character or whatever you are updating in the case.

01:19.090 --> 01:23.140
We are not updating password in our case, but all the validators will run through itself.

01:23.140 --> 01:27.040
So obviously we are going to go ahead and use to update the user details.

01:27.040 --> 01:30.280
So let's go ahead and take example and help of this one.

01:30.280 --> 01:31.600
Find by ID an update.

01:31.600 --> 01:32.710
Let's move back up here.

01:32.860 --> 01:35.890
So again, just like all of this, we are going to go ahead and copy this.

01:35.980 --> 01:42.700
And just like always, I'll shrink this one, get some space, and we're going to go ahead and call

01:42.700 --> 01:45.850
this one and simply say update user details.

01:45.850 --> 01:48.190
So seems like a decent name this time.

01:48.190 --> 01:49.780
Let's go ahead and work on with that.

01:49.930 --> 01:56.320
I'm going to go ahead and select all of this and we'll just remove this one since we are going to use

01:56.740 --> 01:58.150
the cookie as a token.

01:58.150 --> 02:04.090
So I think we don't need to update this cookie token this time because we are not changing password

02:04.090 --> 02:07.180
or any sensitive information, but maybe you want to send it.

02:07.180 --> 02:07.900
It's up to you.

02:07.930 --> 02:10.990
We'll be sending just a response that yeah, it is true totally on you.

02:10.990 --> 02:13.270
You might want to update the cookie token in this case.

02:13.270 --> 02:14.860
So totally 100% valid.

02:15.070 --> 02:19.180
So all it says is first we need to hold a reference of this user.

02:19.180 --> 02:23.590
So we're going to go ahead and just like always, we are going to say user database is always somewhere

02:23.590 --> 02:23.890
else.

02:23.890 --> 02:25.900
So we're going to go ahead and use this one.

02:25.900 --> 02:31.060
So this user had a simple method which says Find by ID and update.

02:31.090 --> 02:32.440
Now this fine by ID.

02:32.440 --> 02:35.080
First thing is we need to pass on the user itself.

02:35.080 --> 02:40.360
So we're going to go ahead and say request user ID, I can extract the ID anywhere I like thanks to

02:40.360 --> 02:41.140
the middleware.

02:41.350 --> 02:44.320
Then we need some of the options that need to be updated.

02:44.320 --> 02:47.080
So user obviously will send me some of the new data.

02:47.080 --> 02:49.210
As of now, I'm not thinking about what new data.

02:49.210 --> 02:54.220
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

02:54.220 --> 02:55.000
for now.

02:55.000 --> 02:59.110
So we're going to go ahead and say, hey, whatever the new data actually comes in, you just go ahead

02:59.110 --> 03:00.550
and update this new data.

03:00.640 --> 03:04.540
Now, further down the road, I'll also update a few options in this method.

03:04.540 --> 03:11.080
The first one is new, which we just study that I need to grab whatever the new updated user data is.

03:11.080 --> 03:18.490
I also want to run the validator, so I'm going to go ahead and say Run Validator Validators, thanks,

03:18.490 --> 03:20.560
and we're going to go ahead and turn on true.

03:20.590 --> 03:28.060
Now optionally, you can also go ahead and pass on this flag which is used to find use.

03:29.260 --> 03:30.490
Come on, suggest me.

03:30.530 --> 03:34.030
No, it's not going to say use, find and modify.

03:34.060 --> 03:36.640
Now, this flag is a little bit controversial.

03:36.640 --> 03:39.070
I always turn it off as a as a false.

03:39.070 --> 03:43.750
In some of the previous versions of MongoDB, maybe your database is not that much updated.

03:43.750 --> 03:45.820
They were using this find and modify.

03:45.850 --> 03:48.370
Now the find and modify is not being used that much.

03:48.370 --> 03:51.130
We rather prefer find by ID and update.

03:51.130 --> 03:53.380
So that's why optionally you can turn this flag off.

03:53.380 --> 03:56.620
But I guess in the most latest version it is already off.

03:56.620 --> 04:02.830
So again, just check it out again and let me know also, okay, once this is all done, this is all

04:02.830 --> 04:05.200
kind of a backward compatibility that we are doing.

04:05.830 --> 04:08.680
Once this is all done and updated, then it's up to you.

04:08.680 --> 04:11.800
Maybe you want to send a cookie token, maybe you want to update that.

04:12.070 --> 04:17.650
Maybe you want to just send a simple resident status and status is going to be 200 because everything

04:17.650 --> 04:18.460
is okay.

04:18.580 --> 04:23.950
We're going to go ahead and simply send a JSON and let's go ahead and say success as true.

04:23.950 --> 04:26.170
Now, this is where the interesting thing comes in.

04:26.170 --> 04:30.400
Sometimes your front end developer is going to say that, hey, I don't need just the success.

04:30.400 --> 04:33.550
True, I probably need the entire user as well at that time.

04:33.550 --> 04:38.110
You can just go ahead and update your back end and say, okay, now you get the full updated user.

04:38.110 --> 04:43.990
Again, this is all about how you are having a talk with the front end developer or what's the requirement.

04:44.050 --> 04:48.130
But the problem is right now that user might be updating three things that we are going to allow.

04:48.130 --> 04:51.280
As of now, he can update his name, email, password.

04:51.280 --> 04:53.470
We have already taken care so we will not take care here.

04:53.470 --> 04:55.960
Or maybe he wants to update the photo as well.

04:55.990 --> 04:59.410
Maybe you have more data, you might want to go ahead and do that.

05:00.040 --> 05:06.670
So in this field, let's just say anyways, we expect that however, whatever the front end guy is doing,

05:06.670 --> 05:11.620
he loads the existing data and user is allowed to change whatever the data is changing.

05:11.620 --> 05:14.590
But also send me all the data at the time of Updation.

05:14.590 --> 05:17.920
If you are not going to send me name or something, it's going to be really bad.

05:17.920 --> 05:22.960
So in the form itself, while the time of editing just load all the data and send me all the data at

05:22.960 --> 05:23.620
this time.

05:23.710 --> 05:29.830
So here we are going to say, hey, request dot, body, dot name if the name is existing because we

05:29.830 --> 05:34.450
are assuming everything is updated, if the values are same, that's how we are going to take care.

05:34.780 --> 05:36.880
And we are also going to say about email.

05:36.880 --> 05:43.660
So request dot, body, dot, email in case you have more fields, then go ahead and just add that.

05:43.780 --> 05:47.320
Now the problem comes in is the image itself.

05:47.350 --> 05:53.950
Now it would be really great if we would have images right up in in our folder structure, in our application

05:53.950 --> 05:54.430
itself.

05:54.430 --> 05:58.000
But we don't have, we have the photos in the cloud.

05:58.000 --> 06:03.370
Netty So what we've got to do first we have to check whether the file is coming in so photo is coming

06:03.370 --> 06:04.120
in or not.

06:04.120 --> 06:06.340
So we're going to go ahead and put up a check.

06:06.340 --> 06:16.450
So we're going to say that, hey, if the request dot files dot photo is not equals to empty or you

06:16.450 --> 06:20.620
can just check for if the request for point file is present or not.

06:20.620 --> 06:26.860
So if this is present, like in this case, if it is not empty, that means you are sending me something.

06:27.190 --> 06:33.790
And in that case I need to delete the previous photo and I need to upload whatever you are sending to

06:33.790 --> 06:34.300
me.

06:34.750 --> 06:35.770
So that's interesting.

06:35.770 --> 06:39.790
So first and foremost, let's go ahead and find the user itself.

06:39.790 --> 06:46.690
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

06:46.690 --> 06:51.730
request dot, user dot ID, let's hold this reference into a variable.

06:51.730 --> 06:55.360
So we're going to go ahead and say user and of course await.

06:56.510 --> 06:57.260
There we go.

06:57.290 --> 07:00.140
Now I have the ID of this user.

07:00.140 --> 07:03.620
Through this ID, I would like to access the photo.

07:04.340 --> 07:07.880
Let's go ahead and take a look on the model because it's a little bit confusing here.

07:07.880 --> 07:14.720
So I have now this ID, so I have get all this information now inside the photo I would like to access

07:14.720 --> 07:19.100
the ID so so the request, whatever the user is, user photo ID.

07:19.460 --> 07:20.630
So that's the next one.

07:21.110 --> 07:21.620
Okay.

07:22.010 --> 07:26.150
So what this will give me, this will give me the idea of the image.

07:26.150 --> 07:32.390
So let's go ahead and say, hey, I want to go ahead and say user dot photo dot ID.

07:32.390 --> 07:35.090
So this will give me the ID of that particular photo.

07:35.090 --> 07:36.500
Let's store that into available.

07:36.500 --> 07:39.020
Also, let's call this one as image ID.

07:39.050 --> 07:39.800
There we go.

07:40.880 --> 07:45.860
Once I have this now based on this ID, I can delete the photos in the cloud.

07:46.160 --> 07:47.540
So let's go ahead and do that.

07:47.840 --> 07:53.600
So although I don't need to store that into a variable, but maybe you want to store that for some reason.

07:53.600 --> 07:58.970
So let's go ahead and say, hey, this is going to be my response and all I have to do is await, of

07:58.970 --> 08:01.700
course, because cloud data is also on another continent.

08:01.850 --> 08:03.440
Let's go ahead and say this one.

08:03.440 --> 08:09.050
So again, take health from cloud and we have already imported that we should have done the V two there

08:09.050 --> 08:09.470
itself.

08:09.470 --> 08:17.000
But anyways and again the same uploader method has a method that says destroy and all it takes is this

08:17.000 --> 08:17.690
image ID.

08:17.720 --> 08:19.430
We don't need to pass on the path itself.

08:19.430 --> 08:23.060
It can find the path automatically and we can delete that.

08:23.060 --> 08:27.860
Now this photo is deleted, so let's upload the photo that user has passed on to me.

08:27.950 --> 08:33.470
So all I'm going to do is let's go ahead and copy some data because deleting part was important.

08:33.500 --> 08:37.310
Uploading is not that much because we have already done that in the sign up part.

08:37.520 --> 08:39.320
So let's go ahead and scroll that.

08:39.320 --> 08:42.350
So this is we have the request files dot photo.

08:42.410 --> 08:44.420
So this is all what we need.

08:44.420 --> 08:48.140
So let's go ahead and copy this and go back up here.

08:49.230 --> 08:51.370
And here we are updating the photo.

08:51.390 --> 08:53.130
Don't worry, I'll write the comments for you.

08:54.000 --> 08:54.570
So.

08:54.570 --> 08:56.070
Or in fact, I can write it up here.

08:56.070 --> 09:07.830
So this one is delete photo on cloud nary and this one is upload the new photo.

09:09.240 --> 09:14.010
So this is uploading the new photo and we are providing the all parameters just like the last one.

09:14.010 --> 09:15.990
So we are holding that into the result.

09:15.990 --> 09:20.670
So result will again give me the two things, the ID and the security URL itself.

09:20.910 --> 09:22.920
So what we're going to go ahead and do this.

09:22.920 --> 09:25.710
We are going to update this object new data.

09:25.710 --> 09:29.280
So let's go ahead and add a new property onto this new data.

09:29.400 --> 09:36.330
So we're going to go ahead and say, hey, new data, you will have a new property photo and this new

09:36.330 --> 09:38.150
photo is going to look like this.

09:38.160 --> 09:40.320
It will have a property of ID.

09:40.500 --> 09:44.070
Now this ID is going to be fulfilled by whatever the result I'm getting.

09:44.070 --> 09:48.620
So result dot and this one is I guess public ID.

09:48.630 --> 09:49.080
Yep.

09:49.080 --> 09:49.560
Thanks.

09:49.560 --> 09:50.220
Suggestion.

09:50.220 --> 09:57.840
I love them and we need to hold the security url so security url is going to come in from result dot

09:57.840 --> 09:59.400
secure url.

09:59.400 --> 10:01.950
Let me verify that one more time again in the model.

10:02.040 --> 10:05.910
So inside the model we have ID and we have secure URL type string.

10:05.910 --> 10:06.510
Absolutely.

10:06.510 --> 10:07.260
Fantastic.

10:07.260 --> 10:13.050
So now if we have the photo inside is coming in or traveling in, then only we are going to update that.

10:13.050 --> 10:14.100
Otherwise we will not.

10:14.720 --> 10:14.870
Okay.

10:14.910 --> 10:16.110
So looks great.

10:16.380 --> 10:20.070
And anyways, we are storing that and this looks great.

10:21.510 --> 10:24.210
So again, depends on how you are performing that.

10:24.210 --> 10:24.960
Totally up to you.

10:24.960 --> 10:27.780
What are the fields that you are changing or sending it to me?

10:27.870 --> 10:30.090
So let's go ahead and create a root for this one.

10:30.090 --> 10:33.630
So update details, this will go into roots.

10:33.870 --> 10:36.150
Let's go ahead and duplicate this one.

10:36.780 --> 10:39.150
And first, let's bring it up here.

10:39.840 --> 10:40.560
There we go.

10:40.560 --> 10:41.490
Save that.

10:43.250 --> 10:45.740
Save that and what is the root?

10:45.770 --> 10:47.240
That's interesting question.

10:47.570 --> 10:50.570
Let's go ahead and say that slash user dashboard slash update.

10:50.570 --> 10:52.580
I would love to copy this entirely.

10:52.610 --> 10:53.450
Copy this.

10:54.020 --> 10:57.200
Go back up here and this will be pasted up here.

10:58.850 --> 10:59.180
Okay.

10:59.180 --> 11:04.730
So now I have a new root, which is slash user dashboard slash update where I am allowed to update the

11:04.730 --> 11:06.200
fields, whatever I'm sending in.

11:06.230 --> 11:07.580
Let's go ahead and test it out.

11:08.020 --> 11:08.360
Okay.

11:08.360 --> 11:10.910
So let's go ahead and save this one.

11:11.300 --> 11:15.230
And this one is going to be user details.

11:15.230 --> 11:17.400
Update, update.

11:18.390 --> 11:18.700
Okay.

11:18.710 --> 11:26.180
So this report is going to go ahead and change like this, slash user dashboard slash update since we

11:26.180 --> 11:29.270
are still logged in because we have the cookies and everything.

11:29.270 --> 11:31.400
So it should be able to extract all these things.

11:31.400 --> 11:36.770
So let's go ahead and send the name and the email and that's it.

11:36.770 --> 11:39.740
We will not send the photo because that is optionally being checked.

11:39.890 --> 11:41.990
So we're going to go ahead and say, Hey, name.

11:42.530 --> 11:44.530
So the name of the user is two.

11:44.540 --> 11:49.310
So let's go ahead and update that as to AE and please don't send the empty email.

11:49.310 --> 11:54.040
Otherwise you will find nothing inside the body and it will just updated with the empty one.

11:54.050 --> 11:55.460
Really don't want to do that.

11:55.790 --> 12:00.740
Probably we want to put a check that there should be something inside the email and the body.

12:00.740 --> 12:07.520
So we should be doing that and the email is going to be same to at the rate ae dot dev.

12:07.820 --> 12:09.890
So again, all these data needs to travel.

12:09.890 --> 12:14.690
Let's go ahead and send this one and it says cannot read the property.

12:16.140 --> 12:17.440
A photo.

12:18.260 --> 12:20.600
So this needs to have a photo as well.

12:22.070 --> 12:22.310
Okay.

12:22.310 --> 12:23.720
Let's go ahead and check it out.

12:25.930 --> 12:26.210
Okay.

12:26.230 --> 12:31.060
So this one says cannot read the property photo of and defines a photo we are not sending in.

12:31.480 --> 12:32.770
So let's go into the middle.

12:32.770 --> 12:35.200
Where it's not middle, it's the controllers.

12:36.620 --> 12:40.220
And we need to check if the request files are coming in first.

12:40.220 --> 12:46.940
So if the requested files are there, then only we can actually extract the request by bat.

12:46.970 --> 12:48.860
This is a file.

12:49.160 --> 12:50.360
This shouldn't be file.

12:51.260 --> 12:53.690
This should be over a photo.

12:54.980 --> 12:56.720
Are we calling it as photo?

12:57.440 --> 13:00.380
Previously we are storing that into a into a variable.

13:00.380 --> 13:11.420
That's why the problem is so it should be this time request dot files, dot photo, photo, dot temp

13:11.420 --> 13:11.720
path.

13:11.720 --> 13:13.730
And again we are checking this.

13:13.730 --> 13:17.930
If the request files dot photo exist then only then that step path.

13:17.930 --> 13:19.400
So it should be good.

13:19.400 --> 13:21.830
Now let's try it one more time.

13:22.370 --> 13:24.250
So this time we are sending name and email.

13:24.260 --> 13:25.760
Let's go ahead and send it again.

13:25.760 --> 13:28.340
And we are still receiving the error that I cannot read.

13:28.340 --> 13:35.420
The property of the photo cannot read photo of undefined because we are not sending request files.

13:35.420 --> 13:42.950
So we can actually go ahead and say if the request files exist, then we'll try to run all of this and

13:42.950 --> 13:46.760
we'll try to extract this request, dot files, dot photo.

13:46.760 --> 13:51.200
Then only if the files exist they should be coming up in the photo itself.

13:51.230 --> 13:56.270
We can actually strengthen our code a little bit more, but again, we'll try it one more time.

13:56.300 --> 13:58.010
Let's go ahead and send this one.

13:59.050 --> 14:01.420
As the raw data at this time.

14:01.420 --> 14:02.420
We got the success through.

14:02.450 --> 14:03.460
Again, you get the idea.

14:03.460 --> 14:07.870
The most important thing is you should be able to understand why the error is coming in and what more

14:07.870 --> 14:09.490
you can do to handle that error.

14:09.670 --> 14:12.400
But again, we need to check it up into the DB as well.

14:12.400 --> 14:17.410
Let's go ahead and hit a refresh on this one and now we got the name being updated as to space.

14:17.410 --> 14:18.760
AE Fantastic.

14:18.760 --> 14:22.900
So this is definitely a much more strengthened code.

14:23.530 --> 14:25.810
Really a mouthful there, but there we go.

14:25.810 --> 14:28.540
Now again, we can go ahead and put up a simple check.

14:28.540 --> 14:38.500
I'll just add it to do for you here that add a check for email and name in the body.

14:38.500 --> 14:39.790
So go ahead and make a check.

14:39.790 --> 14:45.040
If these are not there in the body, then go ahead and generate a custom error and simply say that hey,

14:45.070 --> 14:49.810
email and an email and name should be compulsory, should be available, something like that.

14:50.320 --> 14:52.180
So quite a fun, quite interesting one.

14:52.180 --> 14:56.140
But all of our user controller is half done.

14:56.140 --> 14:57.970
Yes, there is a little bit more on to that.

14:57.970 --> 15:00.040
Let's go ahead and catch up in the next video.
