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 ...