So let's now store all that data in a database.
Again, for that, I do have my database connection file here.
I'm establishing a database connection
whenever we start this server.
And hence in this routes file,
we can now also require
our own data database file
like this, just as we did it in the previous core section,
when we worked with MongoDB.
So that we now can go down here.
And instead of logging that data, we can now use DB,
get DB to get access to the database,
then get access to a specific collection.
Here I'll name it users.
By the way, the database name
to which I'm connecting is file demo.
It will be created automatically if it doesn't exist yet.
And then here on this users collection,
we can insert one new document.
And we can describe this new document here
by creating an object on the fly with curly braces.
And here we can have the user name, so a name field,
by accessing user data dot username.
Because that user data,
which we logged before, does have this username field.
And then, we also want to store the image path here,
and we can get that from the uploaded image file object.
If we have a look at the log from before,
we see that here, we have a full path to that file,
and that's exactly what we need.
So, thanks to Multer, we have easy access to this path
and we can restore uploaded image file dot path
here in the database.
So not the image, but a path to the image.
Now, just as before, this is an asynchronous operation.
So we should add async here in front of this function,
so that here we can use a wait,
and wait for this operation to finish,
before we then redirect back to the starting page.
With all of that out of the way,
if I now go back
and I add a new user named Maximilian,
and I choose a file, same file as always here in my case,
and click save user.
This should have been stored.
And of course we can confirm this with the MongoDB shell.
For this I'll use the file demo database,
which is the database to which we're connecting,
as I mentioned a couple of minutes ago.
And then here, we can access the users collection,
which is that collection
into which we're inserting our document.
And call find there to find all the documents.
And there, I see my user with that name,
with the automatically generated ID,
and with that image path.
So that's working, and that's now how we're storing
such a file correctly.
Next, we want to output that data here
on this starting page.
And since I also want to output the image here,
we also have to ensure that the image
is then served correctly.