1
00:00:02,190 --> 00:00:06,050
So let's now store all that data in a database.

2
00:00:06,050 --> 00:00:10,220
Again, for that, I do have my database connection file here.

3
00:00:10,220 --> 00:00:12,420
I'm establishing a database connection

4
00:00:12,420 --> 00:00:14,720
whenever we start this server.

5
00:00:14,720 --> 00:00:17,120
And hence in this routes file,

6
00:00:17,120 --> 00:00:20,670
we can now also require

7
00:00:20,670 --> 00:00:23,540
our own data database file

8
00:00:23,540 --> 00:00:27,470
like this, just as we did it in the previous core section,

9
00:00:27,470 --> 00:00:29,123
when we worked with MongoDB.

10
00:00:29,987 --> 00:00:32,340
So that we now can go down here.

11
00:00:32,340 --> 00:00:36,190
And instead of logging that data, we can now use DB,

12
00:00:36,190 --> 00:00:38,923
get DB to get access to the database,

13
00:00:39,950 --> 00:00:43,490
then get access to a specific collection.

14
00:00:43,490 --> 00:00:45,333
Here I'll name it users.

15
00:00:46,350 --> 00:00:47,890
By the way, the database name

16
00:00:47,890 --> 00:00:50,083
to which I'm connecting is file demo.

17
00:00:50,920 --> 00:00:54,003
It will be created automatically if it doesn't exist yet.

18
00:00:56,270 --> 00:00:58,360
And then here on this users collection,

19
00:00:58,360 --> 00:01:01,260
we can insert one new document.

20
00:01:01,260 --> 00:01:03,620
And we can describe this new document here

21
00:01:03,620 --> 00:01:08,030
by creating an object on the fly with curly braces.

22
00:01:08,030 --> 00:01:11,810
And here we can have the user name, so a name field,

23
00:01:11,810 --> 00:01:15,183
by accessing user data dot username.

24
00:01:18,671 --> 00:01:19,860
Because that user data,

25
00:01:19,860 --> 00:01:23,363
which we logged before, does have this username field.

26
00:01:24,770 --> 00:01:28,720
And then, we also want to store the image path here,

27
00:01:28,720 --> 00:01:32,053
and we can get that from the uploaded image file object.

28
00:01:33,020 --> 00:01:35,360
If we have a look at the log from before,

29
00:01:35,360 --> 00:01:38,900
we see that here, we have a full path to that file,

30
00:01:38,900 --> 00:01:40,563
and that's exactly what we need.

31
00:01:42,730 --> 00:01:46,490
So, thanks to Multer, we have easy access to this path

32
00:01:46,490 --> 00:01:49,900
and we can restore uploaded image file dot path

33
00:01:49,900 --> 00:01:51,430
here in the database.

34
00:01:51,430 --> 00:01:54,803
So not the image, but a path to the image.

35
00:01:57,010 --> 00:02:00,720
Now, just as before, this is an asynchronous operation.

36
00:02:00,720 --> 00:02:04,620
So we should add async here in front of this function,

37
00:02:04,620 --> 00:02:07,710
so that here we can use a wait,

38
00:02:07,710 --> 00:02:10,070
and wait for this operation to finish,

39
00:02:10,070 --> 00:02:12,963
before we then redirect back to the starting page.

40
00:02:15,180 --> 00:02:17,940
With all of that out of the way,

41
00:02:17,940 --> 00:02:19,600
if I now go back

42
00:02:20,730 --> 00:02:24,290
and I add a new user named Maximilian,

43
00:02:24,290 --> 00:02:28,830
and I choose a file, same file as always here in my case,

44
00:02:28,830 --> 00:02:30,410
and click save user.

45
00:02:30,410 --> 00:02:32,720
This should have been stored.

46
00:02:32,720 --> 00:02:36,480
And of course we can confirm this with the MongoDB shell.

47
00:02:36,480 --> 00:02:39,970
For this I'll use the file demo database,

48
00:02:39,970 --> 00:02:41,940
which is the database to which we're connecting,

49
00:02:41,940 --> 00:02:44,550
as I mentioned a couple of minutes ago.

50
00:02:44,550 --> 00:02:49,070
And then here, we can access the users collection,

51
00:02:49,070 --> 00:02:50,340
which is that collection

52
00:02:50,340 --> 00:02:52,583
into which we're inserting our document.

53
00:02:54,600 --> 00:02:57,840
And call find there to find all the documents.

54
00:02:57,840 --> 00:03:00,440
And there, I see my user with that name,

55
00:03:00,440 --> 00:03:02,930
with the automatically generated ID,

56
00:03:02,930 --> 00:03:04,943
and with that image path.

57
00:03:06,160 --> 00:03:09,260
So that's working, and that's now how we're storing

58
00:03:09,260 --> 00:03:10,893
such a file correctly.

59
00:03:12,080 --> 00:03:15,010
Next, we want to output that data here

60
00:03:15,010 --> 00:03:16,690
on this starting page.

61
00:03:16,690 --> 00:03:19,270
And since I also want to output the image here,

62
00:03:19,270 --> 00:03:21,960
we also have to ensure that the image

63
00:03:21,960 --> 00:03:23,563
is then served correctly.

