title: Image uploading platform using node-js, mongoose and multer
body: <p>I'm desperately trying to build an image gallery in node-js where I can:
  1. use a form to upload an image from the harddrive
  2. save the uploaded file in mongo-DB (mongoose)
  3. Retrieve the uploaded images and show it in my website</p>
  
  <p>I'm quite new to node and also mongodb, so I have a really hard time doing this (doing the same for simply news / text-only db-schemas was so easy). I have searched all similar topics before, but none of them helped me, so I'm really desperate now since I had to copy some code-parts that I don't understand on the way to solving it. Please help me with some beginners explanations!</p>
  
  <p>My html form</p>
  
  <pre><code>        &lt;form id="uploadForm" action="/images" method="POST" enctype="multipart/form-data"&gt;
          &lt;div class="form-group"&gt;
              &lt;label for="imageSelector"&gt;Select Image&lt;/label&gt;
              &lt;input type="file" class="form-control" name="image" placeholder="upload image" id="imageSelector" accept="image/png, image/jpeg"&gt;
          &lt;/div&gt;
          &lt;div class="form-group imagePreview"&gt;
              &lt;p&gt;No files currently selected for upload&lt;/p&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label for="imageDescription"&gt;Image Description&lt;/label&gt;
              &lt;input type="text" class="form-control" name="description" placeholder="description" id="imageDescription"&gt;
          &lt;/div&gt;
              &lt;button class="btn btn-success"&gt;submit&lt;/button&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p>My DB-Schema:</p>
  
  <pre><code>let imageSchema = new mongoose.Schema({
  img:  { data: Buffer, contentType: String },
  description: String});
  
  module.exports = mongoose.model("Image", imageSchema); //Exporting my Schema
  </code></pre>
  
  <p>My route file:</p>
  
  <pre><code>let express = require("express");
  let mongoose = require("mongoose");
  let bodyParser = require("body-parser");
  let router = express.Router({mergeParams: true});
  let multer  = require("multer");
  
  let Image = require("../models/image"); //Using DB-Schema that I exported
  
  const GridFsStorage = require('multer-gridfs-storage');
  
  const storage = new GridFsStorage({
    url: "mongodb://localhost/My_DB",
    file: (req, file) =&gt; {
      return {
        filename: 'file_' + Date.now()
      };
    }
  });
  
  const upload = multer({ storage });
  const sUpload = upload.single('image');
  
  
  router.post('/', sUpload, (req, res, next) =&gt; { 
      console.log("FILENAME: " + req.file.filename);
      console.log("CHUNKSIZE: " + req.file.chunkSize);
      console.log("CONTENTTYPE: " + req.file.contentType);
      Image.create(req.file, function(err, addedImage) {
          if(err) {
              console.log(err);
          } else {
              console.log("SUCCESSFULLY ADDED NEW IMAGE! " + addedImage);
              res.redirect("/images");
          }
      })
  
  });
  
  module.exports = router;
  </code></pre>
  
  <p>Now, what do I have to do to get the actual file to retrieve it? When I load my html-form and upload an image, after clicking submit, I get the following console.logs:</p>
  
  <pre><code>FILENAME: file_1522841638818
  CHUNKSIZE: 261120
  CONTENTTYPE: image/png
  PATH: undefined
  ADDED NEW IMAGE SUCCESSFULLY! { _id: 5ac4b82674d2091a8db36f2f, __v: 0 }
  </code></pre>
  
  <p>When I go to my DB "My_DB" and take a look at db.images.find() the image is not uploaded. So I need to know
  1. How to upload it into my DB?
  2. How to retrieve it and really use the file itself to show it within html ?</p>
  
  <p>Thanks very much!</p>
  
  <p>Btw.: I implemented this whole multer-gridfs-storage thing because I red that you can't upload images > 16 MB without it. I think I'm not really using multer yet so maybe you can give me some hints of how I can get to the image file. I know, you might talk about some docu of specific packages that I implemented. Believe me, this is only one attempt of about 100 different attempts to approach this and I'm really confused now of what is important for me :-/</p>