Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,830 --> 00:00:08,300
Now in the last lesson we solved some of our problems namely how do we get some default items into our
2
00:00:08,300 --> 00:00:16,430
ToDo list to start off without having to use arrays and instead getting this from MongoDB database.
3
00:00:16,520 --> 00:00:18,050
There's just one problem.
4
00:00:18,230 --> 00:00:24,830
If I tried to add a new item right now then my app will crash because again it's telling me there's an
5
00:00:24,830 --> 00:00:33,320
array called items that we used to use down here no longer exists and I can't really push something onto something
6
00:00:33,320 --> 00:00:35,330
that doesn't exist anymore right?
7
00:00:35,510 --> 00:00:43,880
This happens in our post route and that is when we decide to add a new item and we trigger the form
8
00:00:43,910 --> 00:00:51,170
to post to our root route which gets caught in here and we try to grab the item that the user tried
9
00:00:51,170 --> 00:00:56,840
to post and then we try to push it into our items array.
10
00:00:57,560 --> 00:01:01,150
How can we do this using MongoDB instead?
11
00:01:01,490 --> 00:01:07,960
Well for now I'm going to completely ignore the fact that we had a work route and a normal route.
12
00:01:08,000 --> 00:01:12,140
I'm going to simply delete all of this.
13
00:01:12,350 --> 00:01:18,980
We know that when this post route is triggered we can tap into something called req.body.
14
00:01:19,130 --> 00:01:27,410
newItem and that will refer to the text that the user entered into the input when they clicked on the
15
00:01:27,440 --> 00:01:31,580
plus button to add that newItem. That text
16
00:01:31,580 --> 00:01:42,850
I'm going to save into a constant called itemName. And then I need to create a new item document based
17
00:01:42,850 --> 00:01:47,050
off my model in MongoDB. As a challenge
18
00:01:47,050 --> 00:01:54,070
I want you to pause the video and create this newItem document using this itemName that got passed
19
00:01:54,070 --> 00:01:56,760
over.
20
00:01:56,800 --> 00:02:02,230
Okay. So this is exactly the same as what we did up here when we created new documents.
21
00:02:02,260 --> 00:02:10,810
We're going to write const item = new Item so create it from the item model with Mongoose and then
22
00:02:10,810 --> 00:02:16,750
that item only has a single field which is called name and the value we're going to give it is going
23
00:02:16,750 --> 00:02:21,990
to be the item name that we got passed over from our list.
24
00:02:22,020 --> 00:02:23,490
ejs.
25
00:02:23,720 --> 00:02:30,730
Now that we've created our new document, then instead of insertMany or using some of the other insert methods,
26
00:02:31,070 --> 00:02:39,200
we could actually just use the Mongoose shortcut by saying item.save and that will save this item
27
00:02:39,470 --> 00:02:42,240
into our collection of items.
28
00:02:42,260 --> 00:02:52,160
So now if we hit save and we go back to our localhost: 3000 and we go ahead and hit enter to refresh
29
00:02:52,160 --> 00:02:53,490
our web page, then
30
00:02:53,520 --> 00:03:02,960
now if I added a new item here and I hit +, then I head over to my Mongo shell and I say "show dbs"
31
00:03:03,480 --> 00:03:05,440
"use todolist
32
00:03:05,640 --> 00:03:14,790
DB". "db.dot items.find" and you can see I now have 4 items.
33
00:03:14,840 --> 00:03:19,030
So it was successfully saved into my items collection.
34
00:03:20,630 --> 00:03:29,870
In order to get it to show up here all we need to do is just to redirect-- res.redirect back over
35
00:03:30,020 --> 00:03:31,940
to the home route.
36
00:03:32,000 --> 00:03:40,520
So now after we save our item then we re-enter over here and we find all the items inside our items
37
00:03:40,520 --> 00:03:43,600
collection and render it on the screen.
38
00:03:44,150 --> 00:03:47,450
Let's again test our app localhost:3000
39
00:03:47,510 --> 00:03:49,030
let's add a new item
40
00:03:49,040 --> 00:03:54,080
hit +, redirects to the home route and we see it update immediately.
4240
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.