All language subtitles for 009 Reading 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,110 --> 00:00:02,420 Hey, there! 2 00:00:02,420 --> 00:00:06,430 So let's now learn how to read documents with Mongoose 3 00:00:06,430 --> 00:00:10,420 in order to implement or get tour and get all tour's 4 00:00:10,420 --> 00:00:11,333 route handlers. 5 00:00:13,000 --> 00:00:16,470 And starting with the getAllTours handler. 6 00:00:16,470 --> 00:00:20,210 So first off, let's delete this piece of code here. 7 00:00:20,210 --> 00:00:21,530 And now what we're gonna do 8 00:00:21,530 --> 00:00:24,600 in order to get all the tours from the database 9 00:00:24,600 --> 00:00:26,850 is to use Tour. 10 00:00:26,850 --> 00:00:28,390 So the model again. 11 00:00:28,390 --> 00:00:31,490 At the end on there, we use the find method. 12 00:00:31,490 --> 00:00:32,409 All right. 13 00:00:32,409 --> 00:00:35,480 So remember from the MongoDB intersection, 14 00:00:35,480 --> 00:00:38,260 where when we wanted to query for all the documents, 15 00:00:38,260 --> 00:00:41,010 we simply used find, okay? 16 00:00:41,010 --> 00:00:43,670 And so here, it's the exact same method. 17 00:00:43,670 --> 00:00:46,980 And again, when we don't pass anything into it, 18 00:00:46,980 --> 00:00:48,760 so just like we're doing here, 19 00:00:48,760 --> 00:00:50,850 then it will return all the documents 20 00:00:50,850 --> 00:00:52,660 in that collection, okay? 21 00:00:52,660 --> 00:00:56,100 So in this case, in the tour collection obviously. 22 00:00:56,100 --> 00:00:59,260 Now just like before, this is gonna return a promise 23 00:00:59,260 --> 00:01:00,590 that we will await. 24 00:01:00,590 --> 00:01:02,210 And since we wanna await, 25 00:01:02,210 --> 00:01:04,483 we need to make this an async function. 26 00:01:05,580 --> 00:01:06,760 So async. 27 00:01:06,760 --> 00:01:11,760 Then here we can await, and save the result into tours. 28 00:01:15,550 --> 00:01:16,400 All right. 29 00:01:16,400 --> 00:01:17,550 And that's actually it. 30 00:01:18,720 --> 00:01:22,780 So, let's get rid of these comments here. 31 00:01:22,780 --> 00:01:25,840 We also don't want the requestedAt property. 32 00:01:25,840 --> 00:01:28,330 So that was just for testing before. 33 00:01:28,330 --> 00:01:31,580 And so, yeah, I think that's it. 34 00:01:31,580 --> 00:01:33,810 So this is how we get all the tours 35 00:01:33,810 --> 00:01:35,610 from the Tour collection. 36 00:01:35,610 --> 00:01:38,900 So using find, then awaiting that result, 37 00:01:38,900 --> 00:01:41,280 and then sending it down here. 38 00:01:41,280 --> 00:01:44,520 So remember that we have this data property here 39 00:01:44,520 --> 00:01:47,670 to basically envelope the tours. 40 00:01:47,670 --> 00:01:50,380 And then we also send this property here, 41 00:01:50,380 --> 00:01:52,830 which simply measures the number of results 42 00:01:52,830 --> 00:01:55,180 that are in the tours, all right. 43 00:01:55,180 --> 00:01:56,013 Okay? 44 00:01:56,013 --> 00:01:58,700 Because of course, this find method here 45 00:01:58,700 --> 00:02:02,260 will return an array of all these documents, 46 00:02:02,260 --> 00:02:04,720 and will also very nicely convert them 47 00:02:04,720 --> 00:02:06,700 into JavaScript objects, okay? 48 00:02:06,700 --> 00:02:09,060 So this function here, this find method, 49 00:02:09,060 --> 00:02:11,350 really does all the work for us. 50 00:02:11,350 --> 00:02:12,750 Now just like before, 51 00:02:12,750 --> 00:02:16,460 one more step before we can actually try this out, 52 00:02:16,460 --> 00:02:20,030 which is to wrap all of this here in the try block 53 00:02:20,030 --> 00:02:22,673 and then catch any potential errors. 54 00:02:26,160 --> 00:02:26,993 Now in this case, 55 00:02:26,993 --> 00:02:31,000 there are not gonna be any validation errors like before. 56 00:02:31,000 --> 00:02:34,340 But let's still send an error here, in this case, 57 00:02:34,340 --> 00:02:36,700 and let's just make it a 404. 58 00:02:36,700 --> 00:02:39,620 And it doesn't really matter so much at this point 59 00:02:39,620 --> 00:02:41,720 because again, we are gonna implement 60 00:02:41,720 --> 00:02:43,320 some better error handling here 61 00:02:43,320 --> 00:02:46,010 in the next, or in one of the next, sections. 62 00:02:46,010 --> 00:02:47,180 All right? 63 00:02:47,180 --> 00:02:51,407 So let's just set it here to status: 'fail', 64 00:02:52,640 --> 00:02:57,113 and the message to whatever error that we might get here. 65 00:02:59,000 --> 00:03:00,010 Okay. 66 00:03:00,010 --> 00:03:02,470 So, let's test it out now. 67 00:03:02,470 --> 00:03:04,163 It's this one, Get All Tours. 68 00:03:05,210 --> 00:03:08,920 And, yup, it works. 69 00:03:08,920 --> 00:03:12,310 So here we have the array of the three tours 70 00:03:12,310 --> 00:03:15,140 that we currently have in our database. 71 00:03:15,140 --> 00:03:15,973 Perfect. 72 00:03:15,973 --> 00:03:19,193 So next up, let's implement the getTour handler. 73 00:03:20,240 --> 00:03:22,967 So let's write the next one that we have here. 74 00:03:22,967 --> 00:03:25,330 And let's get rid of this code, 75 00:03:25,330 --> 00:03:27,130 and this code. 76 00:03:27,130 --> 00:03:28,470 All right. 77 00:03:28,470 --> 00:03:32,650 And this one is actually gonna work in a very similar way. 78 00:03:32,650 --> 00:03:35,203 So I'm still doing this try catch here. 79 00:03:36,060 --> 00:03:39,570 So error, like this. 80 00:03:39,570 --> 00:03:42,893 Because we're still using async await in this one. 81 00:03:43,960 --> 00:03:44,793 So async. 82 00:03:46,010 --> 00:03:48,370 Because now in order to find one tour, 83 00:03:48,370 --> 00:03:53,160 we are still going to be awaiting Tour.find. 84 00:03:53,160 --> 00:03:55,240 But it's not gonna be just find, 85 00:03:55,240 --> 00:03:58,250 but instead we're gonna use findById, all right. 86 00:04:00,990 --> 00:04:05,240 So findById, but where is that ID actually coming from? 87 00:04:05,240 --> 00:04:06,490 Remember that? 88 00:04:06,490 --> 00:04:09,290 So let's actually take a look at the route. 89 00:04:09,290 --> 00:04:11,890 So going back here to Postman, 90 00:04:11,890 --> 00:04:14,920 this is how we call this end point, 91 00:04:14,920 --> 00:04:19,079 oh, let's actually put a more real ID here already. 92 00:04:19,079 --> 00:04:20,230 So, for example, this one 93 00:04:20,230 --> 00:04:21,572 from The Park Camper. 94 00:04:23,490 --> 00:04:27,400 And so our request might come from a route like this. 95 00:04:27,400 --> 00:04:29,870 And so here, we have this ID parameter 96 00:04:29,870 --> 00:04:32,660 right in our route, right? 97 00:04:32,660 --> 00:04:35,010 So let's take a look at our router here. 98 00:04:35,010 --> 00:04:37,530 And so remember that this is how we specify it, 99 00:04:37,530 --> 00:04:39,730 the ID in the URL. 100 00:04:39,730 --> 00:04:44,010 So this ID is gonna be part of the request obviously. 101 00:04:44,010 --> 00:04:47,530 And so how do we actually get access to that ID here, 102 00:04:47,530 --> 00:04:49,120 and this handler? 103 00:04:49,120 --> 00:04:51,783 Well, we do req.params,id. 104 00:04:56,250 --> 00:04:57,100 And it is ID 105 00:04:57,100 --> 00:05:01,070 because that's what we called this variable here, right? 106 00:05:01,070 --> 00:05:02,610 If it was name here, 107 00:05:02,610 --> 00:05:06,780 then here we had req.params.name, right? 108 00:05:06,780 --> 00:05:07,613 Remember that? 109 00:05:08,960 --> 00:05:12,163 And now, let's save that to tour. 110 00:05:14,020 --> 00:05:14,863 Now all right. 111 00:05:16,400 --> 00:05:18,733 Then let's get this code here back. 112 00:05:20,490 --> 00:05:21,523 Copy it here. 113 00:05:23,420 --> 00:05:25,610 And then also this one for the error block. 114 00:05:25,610 --> 00:05:27,610 And don't worry, I'm gonna explain this function here 115 00:05:27,610 --> 00:05:28,443 in a second. 116 00:05:28,443 --> 00:05:30,553 I just wanna finish this code here first. 117 00:05:32,610 --> 00:05:36,070 So this piece of code here is gonna be exactly the same 118 00:05:36,070 --> 00:05:38,070 for this catch block. 119 00:05:38,070 --> 00:05:39,610 Give it a save now, 120 00:05:39,610 --> 00:05:43,290 and so we get no errors here from eslint. 121 00:05:43,290 --> 00:05:46,240 Now about this find ID method here, 122 00:05:46,240 --> 00:05:50,620 well, remember how up here we just used find, okay? 123 00:05:50,620 --> 00:05:53,980 So find is to find all of the documents. 124 00:05:53,980 --> 00:05:56,270 And then we also have findOne 125 00:05:56,270 --> 00:06:00,570 to find only one document, okay? 126 00:06:00,570 --> 00:06:04,240 And so this findById is really just a shorthand, 127 00:06:04,240 --> 00:06:06,323 so a helper function for, 128 00:06:07,760 --> 00:06:09,700 let me write it here as a comment, 129 00:06:09,700 --> 00:06:11,270 for writing findOne, 130 00:06:13,740 --> 00:06:15,920 and then with the filter object, 131 00:06:15,920 --> 00:06:17,610 which is exactly like we learned 132 00:06:17,610 --> 00:06:19,407 in the MongoDB intersection. 133 00:06:21,030 --> 00:06:23,190 So we have the ID field. 134 00:06:23,190 --> 00:06:28,190 And then we could set that to req.params.id. 135 00:06:28,670 --> 00:06:33,153 So this here would work the exact same way as this, okay? 136 00:06:34,510 --> 00:06:39,510 So _id, because that is what the ID looks like. 137 00:06:39,740 --> 00:06:41,497 So MongoDB is called _id. 138 00:06:42,842 --> 00:06:45,950 And we could of course query for that field, right? 139 00:06:45,950 --> 00:06:49,410 We did exactly that in the last section. 140 00:06:49,410 --> 00:06:52,980 So we specified this filter object here, 141 00:06:52,980 --> 00:06:55,920 and then the property that we're searching for, 142 00:06:55,920 --> 00:06:58,990 and then the value that we want to search for. 143 00:06:58,990 --> 00:07:02,570 So exactly the same again, as we did in the last section. 144 00:07:02,570 --> 00:07:04,360 And this findOne method here 145 00:07:04,360 --> 00:07:07,710 will then only return one of the documents. 146 00:07:07,710 --> 00:07:10,140 Now this findById, as I mentioned before, 147 00:07:10,140 --> 00:07:14,050 is simply a shorthand for having to write this. 148 00:07:14,050 --> 00:07:17,440 So behind the scenes, it's gonna do exactly this. 149 00:07:17,440 --> 00:07:20,640 But Mongoose simply wants to make our life easier. 150 00:07:20,640 --> 00:07:23,610 And therefore, gives us access to an easier method 151 00:07:23,610 --> 00:07:25,230 called findById, 152 00:07:25,230 --> 00:07:28,773 which simply expects the ID that we're looking for. 153 00:07:29,950 --> 00:07:31,070 All right? 154 00:07:31,070 --> 00:07:34,170 So let me actually leave that here, just as a reference. 155 00:07:34,170 --> 00:07:36,513 And so now, this would actually work already. 156 00:07:37,930 --> 00:07:39,720 So, let's take a look at that. 157 00:07:39,720 --> 00:07:43,030 So we already put the ID of this tour. 158 00:07:43,030 --> 00:07:45,730 I think it was this Park Camper. 159 00:07:45,730 --> 00:07:47,003 So ac2. 160 00:07:48,170 --> 00:07:51,140 And ac2, that should be The Park Camper, 161 00:07:51,140 --> 00:07:52,763 and indeed, it is. 162 00:07:53,620 --> 00:07:54,640 Great. 163 00:07:54,640 --> 00:07:57,620 And that is the easiest way of finding 164 00:07:57,620 --> 00:08:00,400 or of querying documents from the database. 165 00:08:00,400 --> 00:08:04,060 There is a ton more that we can do with queries, 166 00:08:04,060 --> 00:08:05,530 and we're gonna do all of that 167 00:08:05,530 --> 00:08:08,390 once we start implementing something like filtering 168 00:08:08,390 --> 00:08:10,770 or sorting or pagination, 169 00:08:10,770 --> 00:08:14,420 and all that good stuff a bit later in this section, 170 00:08:14,420 --> 00:08:16,340 so in a couple of videos from now. 171 00:08:16,340 --> 00:08:17,310 At this point though, 172 00:08:17,310 --> 00:08:21,650 it's enough to simply return all of the documents here 173 00:08:21,650 --> 00:08:22,960 in this route handler. 174 00:08:22,960 --> 00:08:26,460 And then here, simply the one for the matching ID. 175 00:08:26,460 --> 00:08:27,550 And that works now, 176 00:08:27,550 --> 00:08:29,773 and so let's move on to the next one. 13082

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