Create - inserting an image model

We already have the functionality in place in our create function to handle randomly naming and uploading an image file. Now, we need to save that information to MongoDB for the uploaded image.

Let's update the original saveImage function inside controllers/images.js:create and include the functionality to tie it into the database.

Our goal with the saveImage function is twofold. First, we want to make sure that we never save an image with the same randomly generated filename as an already existing image to the database. Second, we want to ensure that we only insert the image into the database after it has been successfully uploaded, renamed, and saved to the filesystem. We are going to make two modifications ...

Get Web Development with MongoDB and Node - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.