1
00:00:03,710 --> 00:00:07,605
Now that we have learnt about Mongoose Population,

2
00:00:07,605 --> 00:00:14,040
and how it enables us to populate one document with information from another document.

3
00:00:14,040 --> 00:00:20,605
In this exercise, we will modify the express server that we have been working on so far.

4
00:00:20,605 --> 00:00:25,580
In the dish schema that we have defined earlier, we had comments.

5
00:00:25,580 --> 00:00:28,365
For the comments, we had the author field

6
00:00:28,365 --> 00:00:31,490
that we use to store the details about the author.

7
00:00:31,490 --> 00:00:40,815
In this exercise, we will turn the author field into a reference to a user document,

8
00:00:40,815 --> 00:00:46,265
and we will use Mongoose population to populate the information into

9
00:00:46,265 --> 00:00:50,330
the dishes document as and when required in order

10
00:00:50,330 --> 00:00:54,440
to supply the information to the client.

11
00:00:54,440 --> 00:00:59,960
Now the use of populate and Mongoose population should be done

12
00:00:59,960 --> 00:01:05,550
judiciously so as not to cause too much of overhead on the server side.

13
00:01:05,550 --> 00:01:06,890
Now in this exercise,

14
00:01:06,890 --> 00:01:09,395
we're going to use it simply to populate the information

15
00:01:09,395 --> 00:01:13,280
into the author field of our comments.

16
00:01:13,280 --> 00:01:18,660
So, let's proceed with the exercise to learn how we use Mongoose population.

17
00:01:18,660 --> 00:01:21,455
To get started on this exercise,

18
00:01:21,455 --> 00:01:25,315
go to the project and open the user.js file.

19
00:01:25,315 --> 00:01:27,730
So, in the user.js file,

20
00:01:27,730 --> 00:01:29,600
we store the user schema.

21
00:01:29,600 --> 00:01:35,515
I'm going to modify the user schema by adding in a couple of more fields in there.

22
00:01:35,515 --> 00:01:38,220
One is the first name,

23
00:01:38,220 --> 00:01:40,070
which will be of

24
00:01:40,070 --> 00:01:48,115
the type string and

25
00:01:48,115 --> 00:01:52,025
the default would be an empty string.

26
00:01:52,025 --> 00:01:56,555
So, the first name as the name implies,

27
00:01:56,555 --> 00:02:03,630
stores the first name for the user and then we'll have another field called last name,

28
00:02:03,630 --> 00:02:06,540
which is also of the same kind.

29
00:02:06,540 --> 00:02:13,540
So, I'm just going to copy these two pieces of information and then copy it in

30
00:02:13,540 --> 00:02:20,735
here and so now our user document will contain,

31
00:02:20,735 --> 00:02:22,840
in addition to the username and password,

32
00:02:22,840 --> 00:02:26,760
username and hush and salt that we have seen earlier,

33
00:02:26,760 --> 00:02:34,450
that is automatically added by the passport local Mongoose module.

34
00:02:34,450 --> 00:02:39,840
We will also have the first name and last name for the user being defined here.

35
00:02:39,840 --> 00:02:43,505
Later on we will see how we would initialize

36
00:02:43,505 --> 00:02:50,765
these values by modifying the user's registration process.

37
00:02:50,765 --> 00:02:52,950
Now once we have completed this,

38
00:02:52,950 --> 00:02:56,599
so this way the user's information

39
00:02:56,599 --> 00:03:00,880
can simply be retrieved by looking up the user document here.

40
00:03:00,880 --> 00:03:05,200
So, now that we have the information about the user in the user document,

41
00:03:05,200 --> 00:03:08,560
going into the dish schema,

42
00:03:08,560 --> 00:03:11,015
so going into dishes.js file.

43
00:03:11,015 --> 00:03:13,260
In the dish schema earlier,

44
00:03:13,260 --> 00:03:18,465
we were storing the author of the document in the form of a string here.

45
00:03:18,465 --> 00:03:22,700
Now we're going to be taking advantage of the fact that we

46
00:03:22,700 --> 00:03:27,425
have the support of mongoose population.

47
00:03:27,425 --> 00:03:33,740
So I'm going to turn the comment field from a string into

48
00:03:33,740 --> 00:03:41,975
mongoose schema types object ID.

49
00:03:41,975 --> 00:03:49,120
So, this way, sorry, wrong field.

50
00:03:49,120 --> 00:03:53,135
I meant to turn the author field into

51
00:03:53,135 --> 00:04:02,295
mongoose schema types object ID.

52
00:04:02,295 --> 00:04:05,390
So, the author field now instead of storing a string,

53
00:04:05,390 --> 00:04:10,835
will have a reference to the user document.

54
00:04:10,835 --> 00:04:14,105
So, when I turn the author field into this type,

55
00:04:14,105 --> 00:04:20,180
then the second property that I defined here will be a reference,

56
00:04:20,180 --> 00:04:25,229
which would be a reference to the user model.

57
00:04:25,229 --> 00:04:27,980
So, this way, we are now going to be

58
00:04:27,980 --> 00:04:31,370
connecting this author field and this author field will

59
00:04:31,370 --> 00:04:37,585
simply store a reference to the ID of the user document,

60
00:04:37,585 --> 00:04:43,790
instead of storing the details about the author in the form of a name.

61
00:04:43,790 --> 00:04:45,100
Now when we do that,

62
00:04:45,100 --> 00:04:48,350
we can use mongoose populate to populate

63
00:04:48,350 --> 00:04:53,115
this information into our dishes document whenever required.

64
00:04:53,115 --> 00:04:58,595
So, with this modification to the dishes schema, in dishes.js file,

65
00:04:58,595 --> 00:05:05,910
we'll now update the dish router to use the mongoose population.

66
00:05:05,910 --> 00:05:09,030
So, going to dishRouter.js.

67
00:05:09,030 --> 00:05:16,120
In dish router, recall that when we were getting a dish here,

68
00:05:16,120 --> 00:05:19,470
now when you get the dish here,

69
00:05:19,470 --> 00:05:23,820
we say dishes find then.

70
00:05:23,820 --> 00:05:26,610
So, right at that point,

71
00:05:26,610 --> 00:05:36,005
we'll say dishes find and we will say after this, populate.

72
00:05:36,005 --> 00:05:41,924
So, we are using the population support in mongoose

73
00:05:41,924 --> 00:05:48,165
and we'll say populate comments author.

74
00:05:48,165 --> 00:05:49,740
So, by stating this,

75
00:05:49,740 --> 00:05:51,060
we are saying when

76
00:05:51,060 --> 00:05:58,750
the dishes document has been constructed to send back the reply to the user,

77
00:05:58,750 --> 00:06:05,810
we're going to populate the author field inside there from the user document in there.

78
00:06:05,810 --> 00:06:09,095
So, this call to the populate will ensure that

79
00:06:09,095 --> 00:06:14,665
the other field will be populated with the information as required.

80
00:06:14,665 --> 00:06:18,565
Similarly, going to the dish ID here,

81
00:06:18,565 --> 00:06:21,660
even in the dish ID, same thing.

82
00:06:21,660 --> 00:06:31,680
We will say populate and comments author added

83
00:06:31,680 --> 00:06:37,320
into the dishes find by id in

84
00:06:37,320 --> 00:06:43,395
the get of the /dish ID endpoint.

85
00:06:43,395 --> 00:06:54,350
Similarly, in the comments also when we retrieve the dish,

86
00:06:54,520 --> 00:07:02,370
we'll say populate comments author here and the

87
00:07:02,370 --> 00:07:09,900
same thing also in the dish router,

88
00:07:09,900 --> 00:07:13,695
dish ID comments, comment ID also.

89
00:07:13,695 --> 00:07:16,530
The bigger populating this information there.

90
00:07:16,530 --> 00:07:22,620
Now of course, what this means is that when you are posting the dish,

91
00:07:22,620 --> 00:07:30,090
earlier you are including the author information in the body of the message.

92
00:07:30,090 --> 00:07:35,120
So, now here when we try to push the comment into that,

93
00:07:35,120 --> 00:07:41,370
so this post corresponds to the dish ID comments field.

94
00:07:41,370 --> 00:07:46,280
So, this is how we were posting a comment to a specific dish.

95
00:07:46,280 --> 00:07:48,570
So, now in this post,

96
00:07:48,570 --> 00:07:53,890
since we are not storing the information about the author anymore,

97
00:07:53,890 --> 00:08:02,400
so what we need to do is when we push the item into the author field there.

98
00:08:02,400 --> 00:08:06,720
So, here when you're populating the information into the dish,

99
00:08:06,720 --> 00:08:10,680
we will have to first-

100
00:08:12,010 --> 00:08:16,430
Recall that the body contains the comment already,

101
00:08:16,430 --> 00:08:21,505
but the author property will not be there in the body of the message in the book,

102
00:08:21,505 --> 00:08:26,020
but depending on which user is posting this information,

103
00:08:26,020 --> 00:08:29,250
we can immediately populate the author field.

104
00:08:29,250 --> 00:08:32,535
Now, how do we know which user is posting this information?

105
00:08:32,535 --> 00:08:38,165
The fact that we have done the verify user here for the post,

106
00:08:38,165 --> 00:08:42,115
means that a specific user is posting this information,

107
00:08:42,115 --> 00:08:44,250
and by doing the verify user,

108
00:08:44,250 --> 00:08:50,415
we would have already loaded in the req.user into the request object.

109
00:08:50,415 --> 00:08:51,925
In the request object,

110
00:08:51,925 --> 00:08:55,565
we can go in and say wreck user,

111
00:08:55,565 --> 00:08:59,010
and then underscore ID here.

112
00:08:59,010 --> 00:09:01,910
So, again let me reiterate this point,

113
00:09:01,910 --> 00:09:05,760
how are we obtaining the author's information here?

114
00:09:05,760 --> 00:09:10,470
Now, recall that we updated the dishes schema,

115
00:09:10,470 --> 00:09:13,875
so that the author field in the comment will simply store

116
00:09:13,875 --> 00:09:20,915
the object ID referring to the user that is posting this comment.

117
00:09:20,915 --> 00:09:24,450
Now, how do we know which user is posting this comment?

118
00:09:24,450 --> 00:09:27,085
Now again, to reiterate this point,

119
00:09:27,085 --> 00:09:31,825
recall that when we verified the user here by calling authenticate verify user,

120
00:09:31,825 --> 00:09:37,590
the passport authorized JWT would have loaded

121
00:09:37,590 --> 00:09:45,120
the user information into the request body in the form of req.user.

122
00:09:45,120 --> 00:09:48,470
So, that user will contain the ID of

123
00:09:48,470 --> 00:09:52,520
the specific user that is actually posting this comment.

124
00:09:52,520 --> 00:09:55,730
So, we have already verified the authenticity of the user,

125
00:09:55,730 --> 00:10:01,400
and so the user ID can simply be obtained by saying req.user.

126
00:10:01,400 --> 00:10:04,400
_ID, and that user's ID,

127
00:10:04,400 --> 00:10:09,380
I will assign this to the author field off the comment.

128
00:10:09,380 --> 00:10:10,990
Now when the comment comes in,

129
00:10:10,990 --> 00:10:13,880
the comment in the body of the request message will

130
00:10:13,880 --> 00:10:17,355
only contain the rating field and the comment field.

131
00:10:17,355 --> 00:10:23,425
Now we don't want to explicitly send the author field anymore from the client side,

132
00:10:23,425 --> 00:10:26,090
instead that should be automatically inserted

133
00:10:26,090 --> 00:10:28,990
on the server side based upon the authenticity of

134
00:10:28,990 --> 00:10:32,180
the user That's the point that I had been

135
00:10:32,180 --> 00:10:36,830
reiterating in this modification that I have done here.

136
00:10:36,830 --> 00:10:43,400
So, that users information is automatically obtained from the req.user that is

137
00:10:43,400 --> 00:10:50,200
loaded into the body of the request message by the authenticate verify user,

138
00:10:50,200 --> 00:10:55,250
which is going to use Passport authenticate with the JWT strategy there.

139
00:10:55,250 --> 00:10:59,795
In addition, now when we receive the updated dish here,

140
00:10:59,795 --> 00:11:03,695
we need to populate the author's information into the dish.

141
00:11:03,695 --> 00:11:05,500
So, at this point,

142
00:11:05,500 --> 00:11:08,675
when we receive the dish here,

143
00:11:08,675 --> 00:11:15,370
we are then going to search the dishes here.

144
00:11:15,370 --> 00:11:20,150
So, we'll say Dishes.findByID

145
00:11:21,000 --> 00:11:28,090
and then supply the dish ID as the parameter here,

146
00:11:28,090 --> 00:11:30,190
so we'll say find by ID,

147
00:11:30,190 --> 00:11:33,175
dish ID, and then,

148
00:11:33,175 --> 00:11:43,405
we need to populate the comments author here,

149
00:11:43,405 --> 00:11:55,600
and then we'll say then dish.

150
00:11:55,600 --> 00:12:04,370
Inside there, we're going to be sending this dish information back to the user here.

151
00:12:04,370 --> 00:12:07,260
So, let me cut that and paste that in here.

152
00:12:07,260 --> 00:12:12,670
So, this modification is required because now I need to populate

153
00:12:12,670 --> 00:12:15,190
the author's information back into

154
00:12:15,190 --> 00:12:18,760
the comment before I can send the current back to the user.

155
00:12:18,760 --> 00:12:22,220
So, this is the additional modification that we need to

156
00:12:22,220 --> 00:12:26,105
do when we use the Mongoose population here.

157
00:12:26,105 --> 00:12:29,950
Similarly, now going into the put,

158
00:12:29,950 --> 00:12:34,450
when we modify a specific comment with the comment ID,

159
00:12:34,450 --> 00:12:40,830
so this is under the dish ID comments comment ID part.

160
00:12:40,830 --> 00:12:42,890
So, when we do the put here,

161
00:12:42,890 --> 00:12:49,230
so we first find the dishes find by id req params dish ID,

162
00:12:49,230 --> 00:12:50,840
then in the dish.

163
00:12:50,840 --> 00:12:57,160
So, the first thing that we check is to ensure that if the dish is not null,

164
00:12:57,160 --> 00:13:01,430
and the dish comments ID is not null,

165
00:13:01,430 --> 00:13:08,665
so we checked to make sure that the comment is indeed present in the dish,

166
00:13:08,665 --> 00:13:12,320
and then when the dish itself is returned,

167
00:13:12,320 --> 00:13:16,385
then we need to search again for

168
00:13:16,385 --> 00:13:21,230
the dish because we need to populate the comments author into the dish.

169
00:13:21,230 --> 00:13:27,950
So here, we'll say dishes.findByID (dish ID),

170
00:13:31,750 --> 00:13:36,880
the reason we need to do one more search is because

171
00:13:36,880 --> 00:13:42,240
we need to populate the comments.author here,

172
00:13:42,240 --> 00:13:46,355
so that's the only reason why we need to do one more search here.

173
00:13:46,355 --> 00:13:50,720
Then when we receive the dish here,

174
00:13:52,260 --> 00:13:57,700
obviously because we just updated the dish so the dish information should be

175
00:13:57,700 --> 00:14:03,640
found in the database,

176
00:14:03,640 --> 00:14:07,490
so that should work fine and then inside there will say

177
00:14:07,490 --> 00:14:12,215
risk status code 200 res set header content type application json,

178
00:14:12,215 --> 00:14:14,960
and then return the dish here,

179
00:14:14,960 --> 00:14:16,740
and then we'll handle error here,

180
00:14:16,740 --> 00:14:19,630
and then the other ones if the dishes

181
00:14:19,630 --> 00:14:24,095
now and also the other errors that we have set up earlier,

182
00:14:24,095 --> 00:14:27,050
they will be handled as usual here.

183
00:14:27,050 --> 00:14:32,790
So, these are the additional changes that we need to make sure when you update the dish,

184
00:14:32,790 --> 00:14:39,175
when you're sending back the updated comment or updated dish,

185
00:14:39,175 --> 00:14:44,485
then we will populate the comment in the dish here.

186
00:14:44,485 --> 00:14:48,160
Similarly, going to the delete here,

187
00:14:48,160 --> 00:14:50,575
and then after the delete the comment,

188
00:14:50,575 --> 00:14:59,310
again we're going to be fetching the dish and populating the author information.

189
00:14:59,310 --> 00:15:01,275
So, let me just copy this part,

190
00:15:01,275 --> 00:15:04,130
and then we'll be doing exactly the same thing here,

191
00:15:04,130 --> 00:15:06,770
so we'll say dish save,

192
00:15:06,770 --> 00:15:16,210
then we're going to be then checking dish.findByID (dish author),

193
00:15:16,210 --> 00:15:19,760
and then we'll populate the comments author,

194
00:15:19,760 --> 00:15:21,925
and then we'll say (then) dish,

195
00:15:21,925 --> 00:15:24,920
and then res.statusCode, and so on,

196
00:15:24,920 --> 00:15:29,350
and the remaining error handling just like before here.

197
00:15:29,350 --> 00:15:33,040
So, with this modification to the dish router,

198
00:15:33,040 --> 00:15:41,420
now the last point that we need to consider is the fact that in the user.js file,

199
00:15:41,420 --> 00:15:43,740
we have now added into fields,

200
00:15:43,740 --> 00:15:49,050
the first name and last name field which by default will be empty strings.

201
00:15:49,050 --> 00:15:51,880
So, when the user is registering,

202
00:15:51,880 --> 00:15:54,670
we should allow the user to supply the first name and

203
00:15:54,670 --> 00:15:58,040
last name in the registration process.

204
00:15:58,040 --> 00:16:00,040
Now, where does that take place?

205
00:16:00,040 --> 00:16:03,025
That takes place in the user's.js.

206
00:16:03,025 --> 00:16:05,390
So, going to users users.js,

207
00:16:05,390 --> 00:16:09,885
when the user posts on the slash signup,

208
00:16:09,885 --> 00:16:13,050
earlier we were only posting the username and password.

209
00:16:13,050 --> 00:16:15,105
In addition to those two,

210
00:16:15,105 --> 00:16:21,785
in the json object that we include in the body of the request message coming in,

211
00:16:21,785 --> 00:16:25,530
the post request message coming in from the client side,

212
00:16:25,530 --> 00:16:29,590
we can also include the first name and last name for the user.

213
00:16:29,590 --> 00:16:33,740
So, when the first name and last name for the user is included,

214
00:16:33,740 --> 00:16:35,590
so what am I going to do here?

215
00:16:35,590 --> 00:16:42,450
So, recall that when you say user.register at this point the user information comes in,

216
00:16:42,450 --> 00:16:45,785
and then you have submitted the username here,

217
00:16:45,785 --> 00:16:49,460
and you have also assigned the password here which will be

218
00:16:49,460 --> 00:16:53,380
turned into the hash and salt by the passport local mongoose.

219
00:16:53,380 --> 00:17:00,000
Now, if there is no error that means that the user's registration was successful,

220
00:17:00,000 --> 00:17:08,740
and so at this point what we will do is we'll say if req.body.

221
00:17:08,740 --> 00:17:13,420
First name. So, which means that the body of the incoming request message,

222
00:17:13,420 --> 00:17:16,345
if it contains the first name,

223
00:17:16,345 --> 00:17:24,770
then we'll say user.firstname is equal to req.body.firstname.

224
00:17:26,160 --> 00:17:29,675
Similarly, for the last name also.

225
00:17:29,675 --> 00:17:32,040
So at this point,

226
00:17:32,040 --> 00:17:34,780
we would have the user available here.

227
00:17:34,780 --> 00:17:40,125
See the user is coming in as the second parameter to this callback function here.

228
00:17:40,125 --> 00:17:43,455
So, we are setting up the first name by changing

229
00:17:43,455 --> 00:17:51,490
the first name property inside the user document here saying, req.body.firstname.

230
00:17:51,490 --> 00:17:55,395
If it exists, then we'll set the user's first name to that.

231
00:17:55,395 --> 00:18:03,220
Similarly, if the req.body.last name is available,

232
00:18:03,220 --> 00:18:09,630
so we'll also update the user's last name as req.body.lastname.

233
00:18:09,770 --> 00:18:16,650
And once we have done these two changes to the first name and the last name,

234
00:18:16,650 --> 00:18:23,160
then we need to save the modification that we have done to the user.

235
00:18:23,160 --> 00:18:25,030
So, we have just updated the user.

236
00:18:25,030 --> 00:18:30,550
So, we'll say user.save then this will

237
00:18:30,550 --> 00:18:37,190
return the error or the user.

238
00:18:37,190 --> 00:18:41,025
So, if the modification has been saved properly,

239
00:18:41,025 --> 00:18:43,765
then it will return the error,

240
00:18:43,765 --> 00:18:49,380
otherwise it will return the user value and this passport authenticate we will

241
00:18:49,380 --> 00:18:55,710
do it inside this user here.

242
00:18:55,710 --> 00:19:00,505
So, we'll say, user.save(err, user).

243
00:19:00,505 --> 00:19:04,740
And then also we need to cross check to make sure that

244
00:19:04,740 --> 00:19:10,660
if there is an error in saving the changes to the user,

245
00:19:10,660 --> 00:19:15,180
then we'll say res status code 500,

246
00:19:15,180 --> 00:19:18,485
so let me copy this from there.

247
00:19:18,485 --> 00:19:23,275
So, we'll say res status code 500,

248
00:19:23,275 --> 00:19:30,220
res set header content type application json and res.jason here.

249
00:19:30,220 --> 00:19:35,995
Then, and we'll return to this point.

250
00:19:35,995 --> 00:19:37,960
If there is no error,

251
00:19:37,960 --> 00:19:40,480
then of course you authenticate the user by calling

252
00:19:40,480 --> 00:19:43,550
passport authenticate with the local to ensure that

253
00:19:43,550 --> 00:19:48,835
the user registration has been successful and this should be

254
00:19:48,835 --> 00:19:56,390
correctly done and when which case you will return this message back to the client side.

255
00:19:56,390 --> 00:20:03,215
We need to close this user.save here.

256
00:20:03,215 --> 00:20:07,520
So, make sure that you close this endpoint correctly.

257
00:20:07,520 --> 00:20:11,005
So, user.save is closed here, and that's it.

258
00:20:11,005 --> 00:20:14,730
These are the changes that we need to make to the user.

259
00:20:14,730 --> 00:20:21,740
So thereby, after the user is registered with the given username and the given password,

260
00:20:21,740 --> 00:20:24,940
then after the user is successfully registered,

261
00:20:24,940 --> 00:20:28,235
then we will set the first name and last name field

262
00:20:28,235 --> 00:20:32,925
of the user document by using these two here.

263
00:20:32,925 --> 00:20:35,900
We want to make sure that the user is successfully

264
00:20:35,900 --> 00:20:39,160
registered before we sent the first name and last name for that.

265
00:20:39,160 --> 00:20:42,540
So, that's why we are carrying out this operation after the user

266
00:20:42,540 --> 00:20:46,360
is successfully registered. That's it.

267
00:20:46,360 --> 00:20:53,785
Let's save the changes and go and check out the server.

268
00:20:53,785 --> 00:20:56,185
After saving the changes,

269
00:20:56,185 --> 00:20:59,980
let's now go to the terminal and

270
00:20:59,980 --> 00:21:06,925
then before I start the server,

271
00:21:06,925 --> 00:21:16,690
let me first check out my MongoDB and delete the user that we have registered earlier.

272
00:21:16,690 --> 00:21:25,640
So, we'll say use confusion and then we'll say db.usersfind.

273
00:21:25,650 --> 00:21:30,690
So, we know that this particular user has been registered earlier,

274
00:21:30,690 --> 00:21:32,580
but when we register that user,

275
00:21:32,580 --> 00:21:35,700
we did not register the first name and last name for the user.

276
00:21:35,700 --> 00:21:39,155
So, I'm going to delete this user and then re- register the user.

277
00:21:39,155 --> 00:21:48,370
So, to do that using the Mongo ripple,I will say db users drop,

278
00:21:48,370 --> 00:21:52,220
and then we'll say db users find,

279
00:21:52,220 --> 00:21:54,620
and that should return an empty.

280
00:21:54,620 --> 00:22:01,685
No users registered there and then we'll exit the Mongo ripple.

281
00:22:01,685 --> 00:22:05,285
And so, once we have removed that registered user,

282
00:22:05,285 --> 00:22:08,760
then, let me start my server.

283
00:22:09,490 --> 00:22:12,275
And once the server is up and running,

284
00:22:12,275 --> 00:22:16,240
let's go to Postman and then register

285
00:22:16,240 --> 00:22:20,930
a new user along with the first name and last name of the user.

286
00:22:20,930 --> 00:22:26,845
Then, they will log in as that user and then we'll look at how

287
00:22:26,845 --> 00:22:31,650
the Mongoose population helps us to populate the information about

288
00:22:31,650 --> 00:22:37,000
the user automatically into the document there.

289
00:22:37,000 --> 00:22:40,029
Now going to Postman,

290
00:22:40,029 --> 00:22:42,660
let me do a sign up of a new user.

291
00:22:42,660 --> 00:22:48,310
So, I'm doing a post localhost: 3000 users sign up.

292
00:22:48,310 --> 00:22:50,715
In the body of the message,

293
00:22:50,715 --> 00:22:54,910
we had the username and the password already in there.

294
00:22:54,910 --> 00:22:59,199
Let me add in two additional fields:

295
00:22:59,199 --> 00:23:09,350
first name, last name.

296
00:23:14,880 --> 00:23:18,530
Then, register that user.

297
00:23:20,850 --> 00:23:23,680
So, once I register the user,

298
00:23:23,680 --> 00:23:26,350
you can see that the registration was successful.

299
00:23:26,350 --> 00:23:29,810
Now, let me login as this user.

300
00:23:29,820 --> 00:23:32,640
So, to log in as the user,

301
00:23:32,640 --> 00:23:37,620
let me do a post and cross check to make sure.

302
00:23:37,620 --> 00:23:40,475
So, I'm doing a post to users login.

303
00:23:40,475 --> 00:23:45,725
Let me cross check and I see that the username and password is correctly typed in there.

304
00:23:45,725 --> 00:23:47,775
So when I log in,

305
00:23:47,775 --> 00:23:53,165
I should be successfully logged in and I should be able to obtain this token there.

306
00:23:53,165 --> 00:24:02,660
Because this token is essential for us to be able to add in a dish to our server site.

307
00:24:02,660 --> 00:24:05,915
So, after you obtain the token,

308
00:24:05,915 --> 00:24:10,250
copy this token string and save it because you will need that in

309
00:24:10,250 --> 00:24:13,220
the authorization header for the post

310
00:24:13,220 --> 00:24:16,910
put and delete operations that you're going to perform later.

311
00:24:16,910 --> 00:24:20,540
So let me copy that token.

312
00:24:20,540 --> 00:24:23,890
Now, normally the way I would keep these tokens is,

313
00:24:23,890 --> 00:24:28,400
that I'll just open a text document and then copy and paste it into the text document.

314
00:24:28,400 --> 00:24:31,190
So that for subsequent postman requests,

315
00:24:31,190 --> 00:24:34,230
I can simply copy this string and then paste it into

316
00:24:34,230 --> 00:24:37,770
the authorization header, if required.

317
00:24:37,770 --> 00:24:44,070
So, let me copy that token and here I have a text document open here.

318
00:24:44,070 --> 00:24:50,815
So, I'm going to paste that string into this text document.

319
00:24:50,815 --> 00:24:57,170
So, here we have the token that we have obtained.

320
00:24:57,170 --> 00:25:03,120
Let's now go and post a dish to our server.

321
00:25:03,120 --> 00:25:05,135
Going back to postman,

322
00:25:05,135 --> 00:25:07,535
let me post a dish to the server.

323
00:25:07,535 --> 00:25:12,690
So, this is where I will choose the post here.

324
00:25:12,690 --> 00:25:21,334
Inside here, I have the dish information that I had used earlier but for the comments,

325
00:25:21,334 --> 00:25:25,345
now, recall that earlier we had author field which was storing a string.

326
00:25:25,345 --> 00:25:28,770
So, all these comments are not valid.

327
00:25:28,770 --> 00:25:35,110
So, I'm going to delete all these comments from submission because,

328
00:25:35,110 --> 00:25:42,570
now, we expect the user to post comments on their own.

329
00:25:42,570 --> 00:25:44,460
When the user posts comments,

330
00:25:44,460 --> 00:25:52,155
we will automatically add in the ID of the user into the author field of the comments.

331
00:25:52,155 --> 00:25:55,390
So, let me post this dish here.

332
00:25:55,390 --> 00:25:57,325
Going to the header,

333
00:25:57,325 --> 00:26:01,550
in the authorization header, I'm going to say,

334
00:26:02,310 --> 00:26:12,785
bearer and then, paste the token and then submit.

335
00:26:12,785 --> 00:26:17,055
I should do a post on that.

336
00:26:17,055 --> 00:26:21,950
So, I will say post and so when I post now,

337
00:26:21,950 --> 00:26:26,785
you see that this dish has been posted on the server side,

338
00:26:26,785 --> 00:26:31,340
and with the comments array being empty at this moment.

339
00:26:31,340 --> 00:26:34,450
So, after I post this dish,

340
00:26:34,450 --> 00:26:37,660
let me copy the ID of this dish.

341
00:26:37,660 --> 00:26:40,835
So, let me copy this ID for the dish because I will

342
00:26:40,835 --> 00:26:44,735
need that to post comments for this dish.

343
00:26:44,735 --> 00:26:47,075
Then, going to my text editor,

344
00:26:47,075 --> 00:26:51,485
I'm going to save that ID of the dish here.

345
00:26:51,485 --> 00:26:54,550
Now, of course, once you build your client side,

346
00:26:54,550 --> 00:26:57,770
your client will automatically have all this information.

347
00:26:57,770 --> 00:27:02,565
So, your client will automatically be able to send the token and so on.

348
00:27:02,565 --> 00:27:06,385
So, you don't need to do this cut-and-paste thing but with postman,

349
00:27:06,385 --> 00:27:11,750
this is the only way that we can add any information to our postman requests,

350
00:27:11,750 --> 00:27:17,185
that go out from postman to the server.

351
00:27:17,185 --> 00:27:22,090
Now, in order to convince ourselves that this dish actually exists,

352
00:27:22,090 --> 00:27:26,310
let me do a get on the local hostcolon:3000/dishes.

353
00:27:26,570 --> 00:27:30,750
When I do a get, you can actually see that

354
00:27:30,750 --> 00:27:34,175
this particular dish exists on the server side.

355
00:27:34,175 --> 00:27:37,600
So, let's now try to post a comment.

356
00:27:37,600 --> 00:27:39,515
So, to post a comment,

357
00:27:39,515 --> 00:27:45,550
let's do a post and we'll say,

358
00:27:49,940 --> 00:27:54,950
localhost:3000/dishes, slash and the ID of the dish that

359
00:27:54,950 --> 00:27:59,910
I just copied, and slash comments.

360
00:27:59,910 --> 00:28:03,090
When you post on the comments,

361
00:28:03,090 --> 00:28:11,285
you need to make sure that in the body we will add in the comment here.

362
00:28:11,285 --> 00:28:13,605
So, a typical comment contains

363
00:28:13,605 --> 00:28:20,555
a rating of say

364
00:28:20,555 --> 00:28:26,140
five and then comment.

365
00:28:29,030 --> 00:28:33,535
So, let me just type in some random comment,

366
00:28:33,535 --> 00:28:34,915
just to demonstrate to you.

367
00:28:34,915 --> 00:28:41,085
So, this should be in the body of the post for the comments and in the header,

368
00:28:41,085 --> 00:28:44,665
we should add the authorization header.

369
00:28:44,665 --> 00:28:50,525
So, for the authorization header, we'll say, bearer.

370
00:28:50,525 --> 00:28:54,875
I need to paste the token here,

371
00:28:54,875 --> 00:28:59,065
pasting the token value that I have saved earlier.

372
00:28:59,065 --> 00:29:02,575
Let's now post this comment.

373
00:29:02,575 --> 00:29:05,265
Then, when the comment is posted,

374
00:29:05,265 --> 00:29:07,705
let's look at the returned value here.

375
00:29:07,705 --> 00:29:09,510
So, as you browse down,

376
00:29:09,510 --> 00:29:14,975
you can see that the dish to which the comment has been added, has been returned.

377
00:29:14,975 --> 00:29:19,300
Notice that the dish information is there but note in particular,

378
00:29:19,300 --> 00:29:22,620
what is contained in the comment that has been posted here.

379
00:29:22,620 --> 00:29:25,740
So, as you can see, you already know that the updated and

380
00:29:25,740 --> 00:29:29,050
created at fields are automatically added in by mongoose.

381
00:29:29,050 --> 00:29:31,900
The rating and the comment that we submitted are

382
00:29:31,900 --> 00:29:34,780
right there but note how the author field

383
00:29:34,780 --> 00:29:40,675
now contains the ID corresponding to the user.

384
00:29:40,675 --> 00:29:47,190
Now, as we saw in the code how the author field information is added, now,

385
00:29:47,190 --> 00:29:49,965
if you do a get on the dishes,

386
00:29:49,965 --> 00:29:52,900
you will notice that this author field will be

387
00:29:52,900 --> 00:29:56,890
automatically filled in by the users information here.

388
00:29:56,890 --> 00:30:02,180
So, let's now do a get on the localhost:3000/dishes.

389
00:30:02,300 --> 00:30:06,820
So, when we now do a get on this point,

390
00:30:06,820 --> 00:30:12,215
you'll now notice that in the dishes here,

391
00:30:12,215 --> 00:30:15,460
right there, the information about the dish is already

392
00:30:15,460 --> 00:30:20,110
present but note how the comment is now constructed.

393
00:30:20,110 --> 00:30:23,500
The comment now contains,

394
00:30:23,500 --> 00:30:27,240
the rating comment fields as we saw earlier,

395
00:30:27,240 --> 00:30:28,780
the updated and created at,

396
00:30:28,780 --> 00:30:32,910
but note what happened to the author field here.

397
00:30:32,910 --> 00:30:36,890
So, when you do a get request because we did a populate on

398
00:30:36,890 --> 00:30:42,230
the server side when the get operation is invoked,

399
00:30:42,230 --> 00:30:45,750
the populate has automatically populated

400
00:30:45,750 --> 00:30:51,390
the author information into position in the author field here.

401
00:30:51,390 --> 00:30:54,215
So, in there, you can see that from the author,

402
00:30:54,215 --> 00:30:56,260
you can now look up the last name and

403
00:30:56,260 --> 00:30:58,970
the first name information automatically from the author field.

404
00:30:58,970 --> 00:31:01,645
So, if you need to construct a comment,

405
00:31:01,645 --> 00:31:04,050
you now have the rating,

406
00:31:04,050 --> 00:31:08,210
the comment, and also the first name and last name of

407
00:31:08,210 --> 00:31:12,485
the author automatically included into this document.

408
00:31:12,485 --> 00:31:15,645
Also, the user name also is included into this document.

409
00:31:15,645 --> 00:31:21,495
So, this is how you can add in information from another document and populate

410
00:31:21,495 --> 00:31:27,905
a second document with that information before you reply back from the server site.

411
00:31:27,905 --> 00:31:32,315
So, this is the use of mongoose population and how we

412
00:31:32,315 --> 00:31:37,580
can automatically populate information into a mongoose document.

413
00:31:37,580 --> 00:31:41,280
With this, we complete this exercise.

414
00:31:41,280 --> 00:31:46,075
In this exercise, we have seen the use of mongoose population and we have also seen

415
00:31:46,075 --> 00:31:51,785
how we can populate information from one document into another document.

416
00:31:51,785 --> 00:31:57,340
Whereby, when we modify the server to do the population for the requests,

417
00:31:57,340 --> 00:32:02,200
mongoose will automatically take care of populating this information for us.

418
00:32:02,200 --> 00:32:04,190
All that we need to do,

419
00:32:04,190 --> 00:32:10,900
is to store the reference to the other document in the form of the object ID,

420
00:32:10,900 --> 00:32:16,240
in the document into which you want to populate this information.

421
00:32:16,240 --> 00:32:18,965
With this, we complete this exercise.

422
00:32:18,965 --> 00:32:25,230
This is a good time for you to do a git-commit with the message, mongoose population.