All language subtitles for 010 Updating Documents_Downloadly.ir_en

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian Download
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:01,100 --> 00:00:04,883 In this video, let's implement document updating. | www.downloadly.ir 2 00:00:06,250 --> 00:00:09,810 And so, here, in our update tour handler, let's start by 3 00:00:09,810 --> 00:00:11,803 making it an async function. 4 00:00:14,650 --> 00:00:18,250 Then, or try catch block, and so I'm doing this 5 00:00:18,250 --> 00:00:22,130 very quickly now, because we're already used to this 6 00:00:22,130 --> 00:00:22,963 at this point. 7 00:00:24,330 --> 00:00:25,180 Right? 8 00:00:25,180 --> 00:00:28,210 So this goes into the try block and then 9 00:00:28,210 --> 00:00:32,439 I simply get this one, here, from the catch block 10 00:00:33,450 --> 00:00:34,693 of this other function. 11 00:00:36,130 --> 00:00:37,653 All right, so. 12 00:00:39,230 --> 00:00:40,660 Here we go. 13 00:00:40,660 --> 00:00:42,430 And so now, what we need to do here 14 00:00:42,430 --> 00:00:44,620 at the beginning of this try block 15 00:00:44,620 --> 00:00:47,140 is to basically query for the document that 16 00:00:47,140 --> 00:00:50,580 we want to update and then update it, all right? 17 00:00:50,580 --> 00:00:51,730 And we can actually do that 18 00:00:51,730 --> 00:00:54,250 all in one command with mongoose. 19 00:00:54,250 --> 00:00:57,160 And, remember, again, that we're gonna update the tour 20 00:00:57,160 --> 00:00:59,370 based on an ID, okay? 21 00:00:59,370 --> 00:01:03,320 So, it's a similar situation like this one here, 22 00:01:03,320 --> 00:01:05,590 where we find by ID. 23 00:01:05,590 --> 00:01:08,130 And now, we can actually do something very similar, 24 00:01:08,130 --> 00:01:11,380 which is find by ID and update. 25 00:01:11,380 --> 00:01:13,713 So again, everything in one query. 26 00:01:14,600 --> 00:01:16,790 So, let's do tour.findByID 27 00:01:20,400 --> 00:01:22,963 and update. 28 00:01:24,150 --> 00:01:27,520 Okay, so, what do we need to pass in here? 29 00:01:27,520 --> 00:01:31,290 Well, of course, the ID, so that we can first find 30 00:01:31,290 --> 00:01:33,313 the document that is to be updated. 31 00:01:34,620 --> 00:01:39,030 So, just like before, that is req.params.ID, 32 00:01:39,030 --> 00:01:42,690 and then the data that we actually want to change. 33 00:01:42,690 --> 00:01:44,750 And that data will be in the body, 34 00:01:44,750 --> 00:01:46,763 just like in the post request. 35 00:01:48,430 --> 00:01:51,330 So req.body, okay? 36 00:01:51,330 --> 00:01:53,470 And then, actually as a third argument, 37 00:01:53,470 --> 00:01:57,050 we can also patch in some options, and we will do that. 38 00:01:57,050 --> 00:02:00,600 And the first option that I want to specify is new 39 00:02:01,540 --> 00:02:03,690 and set it to true. 40 00:02:03,690 --> 00:02:06,760 Because this way, then the new updated document 41 00:02:06,760 --> 00:02:08,870 is the one that will be returned. 42 00:02:08,870 --> 00:02:11,810 And since we want to send back that updated document 43 00:02:11,810 --> 00:02:14,410 to the client, we always want this method 44 00:02:14,410 --> 00:02:18,380 to actually return that new document, okay? 45 00:02:18,380 --> 00:02:22,160 So this way, it actually then makes sense to 46 00:02:22,160 --> 00:02:27,160 await this query and then save it to a new variable. 47 00:02:28,710 --> 00:02:30,720 So then simply call it tour here, 48 00:02:30,720 --> 00:02:34,743 and then again, await the result of this query. 49 00:02:35,640 --> 00:02:38,200 So all of these methods that we've been using on the tour 50 00:02:38,200 --> 00:02:41,923 so far, so find by ID and update, or, 51 00:02:43,220 --> 00:02:47,300 well, not really create, but find by ID or find, 52 00:02:47,300 --> 00:02:51,350 all of these will return queries, so they are query methods 53 00:02:51,350 --> 00:02:53,320 and let me actually show that to you 54 00:02:53,320 --> 00:02:55,700 in the mongoose documentation. 55 00:02:55,700 --> 00:02:58,510 So, up until this point, we didn't have a look at 56 00:02:58,510 --> 00:03:00,300 the documentation so far. 57 00:03:00,300 --> 00:03:01,850 And so, let's quickly do that. 58 00:03:01,850 --> 00:03:05,080 So, on the model, which in our case is tour, 59 00:03:05,080 --> 00:03:07,610 we have all these query methods, right? 60 00:03:07,610 --> 00:03:11,920 So you see find, you see find one, you see find by ID, 61 00:03:11,920 --> 00:03:14,530 which is just a shorthand for find one. 62 00:03:14,530 --> 00:03:17,460 Then you see find by ID and update, 63 00:03:17,460 --> 00:03:19,320 which is the one that we're currently using, 64 00:03:19,320 --> 00:03:22,018 and which, in fact, is just a shorthand for 65 00:03:22,018 --> 00:03:26,440 find one and update, where we're querying for an ID, 66 00:03:26,440 --> 00:03:30,380 so similar to what we had in the last lecture, okay? 67 00:03:30,380 --> 00:03:33,630 And actually we can click on any one of these, 68 00:03:33,630 --> 00:03:36,340 so let's do the one that we're using currently, 69 00:03:36,340 --> 00:03:39,931 and that will then really take us to the API documentation 70 00:03:39,931 --> 00:03:43,560 which shows us how to really use a certain method. 71 00:03:43,560 --> 00:03:46,650 So you see that we passed the ID, so the value, 72 00:03:46,650 --> 00:03:50,550 off the ID to query by, we passed the object with 73 00:03:50,550 --> 00:03:55,320 the data to be updated, and we passed an options object. 74 00:03:55,320 --> 00:03:57,410 And down here we can actually see 75 00:03:57,410 --> 00:03:59,770 the options that we can set, so remember, 76 00:03:59,770 --> 00:04:03,360 we just set new to true, and so as it says here 77 00:04:03,360 --> 00:04:06,570 that will then return the modified document 78 00:04:06,570 --> 00:04:08,480 rather than the original. 79 00:04:08,480 --> 00:04:11,500 Now, another one that we actually want to set to true 80 00:04:11,500 --> 00:04:15,690 is this run validators, so that each time that we update 81 00:04:15,690 --> 00:04:19,160 a certain document, then the validators that we specified 82 00:04:19,160 --> 00:04:22,334 in the schema will run again, okay? 83 00:04:22,334 --> 00:04:26,050 And we will talk a lot about validators and validation 84 00:04:26,050 --> 00:04:27,600 a bit later in the section, 85 00:04:27,600 --> 00:04:31,770 but I'm gonna leave it here for now so that we, basically, 86 00:04:31,770 --> 00:04:32,743 don't forget it. 87 00:04:34,340 --> 00:04:35,530 Okay? 88 00:04:35,530 --> 00:04:38,810 Give it a save now, and actually I wanna go back here 89 00:04:38,810 --> 00:04:40,880 just to show you how many methods 90 00:04:40,880 --> 00:04:43,210 we actually have in mongoose. 91 00:04:43,210 --> 00:04:46,250 So, on the model, we have all of these methods here 92 00:04:46,250 --> 00:04:47,753 that you see in this list. 93 00:04:48,620 --> 00:04:51,300 So for example we used create before, 94 00:04:51,300 --> 00:04:52,833 we also used find. 95 00:04:53,850 --> 00:04:54,683 Okay? 96 00:04:54,683 --> 00:04:57,090 And so the ones that you see here in the top, 97 00:04:57,090 --> 00:04:59,350 they are available right on the model, 98 00:04:59,350 --> 00:05:01,363 and that, in our case, is the tour. 99 00:05:02,300 --> 00:05:04,320 Now, down here you, for example also see 100 00:05:04,320 --> 00:05:08,520 model.prototype.safe for example. 101 00:05:08,520 --> 00:05:10,770 And this one, we actually also already used 102 00:05:10,770 --> 00:05:14,290 right when we first started to work with mongoose. 103 00:05:14,290 --> 00:05:16,650 So I'm not sure if you are familiar with this, 104 00:05:16,650 --> 00:05:20,070 but in java script model.prototype always means 105 00:05:20,070 --> 00:05:23,560 an object created from a class, or in this case, 106 00:05:23,560 --> 00:05:25,330 created from a model. 107 00:05:25,330 --> 00:05:29,010 And so, the save method here, refers to a save method 108 00:05:29,010 --> 00:05:32,530 called on a document and not on a tour. 109 00:05:32,530 --> 00:05:35,370 And remember that that's exactly what we did. 110 00:05:35,370 --> 00:05:37,670 And I think I still have it here as a comment. 111 00:05:40,430 --> 00:05:41,550 Yeah, right here. 112 00:05:41,550 --> 00:05:45,717 So, we had a tour, this new tour variable here 113 00:05:45,717 --> 00:05:48,600 which was created from the tour model, 114 00:05:48,600 --> 00:05:53,220 and so this new tour here got access to the save method 115 00:05:53,220 --> 00:05:57,320 because that is part of the prototype object of this class. 116 00:05:57,320 --> 00:06:00,280 All right, now I don't want to bore you with the 117 00:06:00,280 --> 00:06:02,760 fundamentals of java script, but it is important 118 00:06:02,760 --> 00:06:05,620 in order to understand the documentation, okay? 119 00:06:05,620 --> 00:06:09,320 Because at some point you will need to use documentation, 120 00:06:09,320 --> 00:06:11,520 because mongoose is yute. 121 00:06:11,520 --> 00:06:13,700 And I will actually teach you a lot of the stuff 122 00:06:13,700 --> 00:06:16,520 that's important in there through all of this course, 123 00:06:16,520 --> 00:06:19,240 but sometimes you will still need something that 124 00:06:19,240 --> 00:06:20,740 I didn't show you in this course. 125 00:06:20,740 --> 00:06:23,060 And so it's important that you really understand 126 00:06:23,060 --> 00:06:25,900 the documentation as well, okay? 127 00:06:25,900 --> 00:06:29,360 And so, again, when you see model.prototype here, 128 00:06:29,360 --> 00:06:31,810 you know that the save method here 129 00:06:31,810 --> 00:06:34,800 is going to be available on all of the instances 130 00:06:34,800 --> 00:06:37,330 created through a model, okay? 131 00:06:37,330 --> 00:06:39,160 And so, not the model itself. 132 00:06:39,160 --> 00:06:42,000 So, for example, if you tried tour.save 133 00:06:42,000 --> 00:06:43,660 you wouldn't be able to use it. 134 00:06:43,660 --> 00:06:46,960 It would give you an error, but instead, if you tried save 135 00:06:46,960 --> 00:06:49,700 on a document created through the tour, 136 00:06:49,700 --> 00:06:52,120 then it would, of course, work, okay? 137 00:06:52,120 --> 00:06:53,840 I hope that makes sense. 138 00:06:53,840 --> 00:06:56,670 And, anyway, you see that we have a lot of other stuff, 139 00:06:56,670 --> 00:07:00,310 so we have to query, which we already talked about, 140 00:07:00,310 --> 00:07:04,030 because, for example, find by ID and update, or find, 141 00:07:04,030 --> 00:07:08,320 or find by ID, all of these will return query objects. 142 00:07:08,320 --> 00:07:10,860 And so, later on, when we're gonna implement stuff like 143 00:07:10,860 --> 00:07:15,150 sorting or filtering we will then use this query object. 144 00:07:15,150 --> 00:07:15,983 Okay? 145 00:07:15,983 --> 00:07:19,751 So, let's go back, we kind of got off the track here 146 00:07:19,751 --> 00:07:22,210 by looking at the documentation, 147 00:07:22,210 --> 00:07:24,230 but, again, I think it's really important 148 00:07:24,230 --> 00:07:26,370 to start looking at the documentation, 149 00:07:26,370 --> 00:07:29,670 because a course like this can only take you so far, 150 00:07:29,670 --> 00:07:32,570 and I hope you already know that at this point, okay? 151 00:07:32,570 --> 00:07:35,810 It's really important that you're also able to 152 00:07:35,810 --> 00:07:38,450 find information on your own, and I wanna show you how 153 00:07:38,450 --> 00:07:40,560 to do that also in this course. 154 00:07:40,560 --> 00:07:43,460 Anyways, let's now actually also send 155 00:07:43,460 --> 00:07:46,820 this newly updated tour back to the client, 156 00:07:46,820 --> 00:07:49,690 which, right now, we're not doing, because we have 157 00:07:49,690 --> 00:07:52,810 simply this updated tour here string. 158 00:07:52,810 --> 00:07:56,470 So let's get rid of that and simply send to tour. 159 00:07:56,470 --> 00:08:00,140 And remember that this, in fact, is actually this. 160 00:08:00,140 --> 00:08:03,940 So, the tour property is set to the tour object, 161 00:08:03,940 --> 00:08:07,360 but, thanks to ES6, we no longer have to do that 162 00:08:07,360 --> 00:08:11,160 when the property name has the same name of the value. 163 00:08:11,160 --> 00:08:13,310 Okay, so let's get rid of that. 164 00:08:13,310 --> 00:08:15,413 That makes our code look a bit better. 165 00:08:17,350 --> 00:08:19,373 Okay, and let's now test it. 166 00:08:20,980 --> 00:08:25,980 Back to postman, here we have the updated tour route. 167 00:08:26,079 --> 00:08:27,960 And so, let's actually do it 168 00:08:27,960 --> 00:08:30,983 on this same ID that we used before. 169 00:08:34,309 --> 00:08:35,549 So, this one. 170 00:08:35,549 --> 00:08:38,827 Then we come to the body, here, and let's change-- 171 00:08:39,760 --> 00:08:42,250 Well, what are we gonna change here? 172 00:08:42,250 --> 00:08:46,493 Well, let's change the price to, like, 500. 173 00:08:48,040 --> 00:08:51,383 So, price, 500, 174 00:08:52,630 --> 00:08:55,700 send it, and indeed this gives us 175 00:08:55,700 --> 00:08:58,250 the newly updated object with the price 176 00:08:58,250 --> 00:09:00,730 already set to 500. 177 00:09:00,730 --> 00:09:03,410 And if we now get all tours, then that, of course, 178 00:09:03,410 --> 00:09:05,150 should be reflected here. 179 00:09:05,150 --> 00:09:06,480 And here it is. 180 00:09:06,480 --> 00:09:09,223 But now, let's say that we set it to a string. 181 00:09:13,930 --> 00:09:15,920 Let's take a look at what happens, then. 182 00:09:15,920 --> 00:09:17,930 Well, then we actually get an error. 183 00:09:17,930 --> 00:09:21,090 And that's because we ran the validators again. 184 00:09:21,090 --> 00:09:23,497 So, remember that we have-- 185 00:09:24,900 --> 00:09:25,760 Where is that? 186 00:09:27,720 --> 00:09:28,553 Oh yeah, here. 187 00:09:28,553 --> 00:09:32,070 So here we specify that the validators should be run again. 188 00:09:32,070 --> 00:09:36,130 And so, right now the price is no longer a number 189 00:09:36,130 --> 00:09:38,050 as it is expected, right? 190 00:09:38,050 --> 00:09:41,580 So in a schema we say that the price should be a number, 191 00:09:41,580 --> 00:09:44,580 but, of course, java script, or mongoose, actually, 192 00:09:44,580 --> 00:09:49,290 cannot, basically, convert this string here to a number. 193 00:09:49,290 --> 00:09:50,820 But that is what we expect. 194 00:09:50,820 --> 00:09:53,350 So here it says kind should be number. 195 00:09:53,350 --> 00:09:55,580 And so, therefor, it gives us an error, 196 00:09:55,580 --> 00:09:58,540 simply because we're running the validators again. 197 00:09:58,540 --> 00:10:01,530 So, let's put it back to 500 and then, of course, 198 00:10:01,530 --> 00:10:02,730 it's gonna be back. 199 00:10:02,730 --> 00:10:05,430 All right, now, keep in mind that we're actually 200 00:10:05,430 --> 00:10:07,750 doing a patch request here. 201 00:10:07,750 --> 00:10:11,000 Now, if we were doing a put request, remember, 202 00:10:11,000 --> 00:10:13,670 then you would expect that the original object 203 00:10:13,670 --> 00:10:15,190 would be completely replaced 204 00:10:15,190 --> 00:10:18,150 with the new one that is sent in, okay? 205 00:10:18,150 --> 00:10:20,280 So, in that case it would no longer work 206 00:10:20,280 --> 00:10:22,272 the way we implemented here 207 00:10:22,272 --> 00:10:25,420 because this one simply really updated the fields 208 00:10:25,420 --> 00:10:29,360 that are different here in the body, okay? 209 00:10:29,360 --> 00:10:32,150 But anyways, I think that the patch method 210 00:10:32,150 --> 00:10:34,400 is actually way more useful, and so, 211 00:10:34,400 --> 00:10:38,490 that's gonna be the only one that implementing here. 212 00:10:38,490 --> 00:10:42,300 Cool, so we have three of our four correct operations 213 00:10:42,300 --> 00:10:44,910 implemented, only one more left to go, 214 00:10:44,910 --> 00:10:46,463 which is gonna be delete. | www.downloadly.ir 17008

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.