title: Upload image to MongoDB with Nodejs and express
body: <p>I'm trying to upload a image using multer in nodejs. I configured multer to save uploaded images in the "upload" directory and when the form is submitted, the image is sent to the directory as it should. But I'm trying to save the path to that image (req.file.path) to a mongodb database and this is not working. I'm new to nodejs and mongodb and I can't figure out what the problem is.
  This is the error I get:</p>
  
  <blockquote>
    <p>ObjectParameterError: Parameter "obj" to Document() must be an object, got assets\uploads\uploaded_file-1518264794720.jpg
        at new ObjectParameterError (C:\xampp\htdocs\projets\NodeApp01\node_modules\mongoose\lib\error\objectParameter.js:23:11)
        at model.Document (C:\xampp\htdocs\projets\NodeApp01\node_modules\mongoose\lib\document.js:55:11)
        at model.Model (C:\xampp\htdocs\projets\NodeApp01\node_modules\mongoose\lib\model.js:59:12)
        at new model (C:\xampp\htdocs\projets\NodeApp01\node_modules\mongoose\lib\model.js:3750:13)</p>
  </blockquote>
  
  <p>Here is my index.js:</p>
  
  <pre><code>var express=require('express');
  var app=express();
  var fs=require('fs');
  var multer=require('multer');
  app.set('view engine','ejs');
  app.use(express.static('assets'));
  
  
  var mongo=require('mongoose');
  mongo.connect('mongodb://username:pass@database...');
  
  var schema=new mongo.Schema({
      item:String
  });
  
  var model1= mongo.model('todomod',schema);
  
  var storage = multer.diskStorage({
      destination: 'assets/uploads/',
      filename: function (req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now()+ '.jpg')
      }
  })
  
  var upload = multer({ storage: storage });
  
  app.get('/',function(request,response){
      response.render('index.ejs');
  });
  
  app.get('/add_item',function(request,response){
      response.render('add_item.ejs');
  });
  
  app.post('/add_item',upload.single('uploaded_file'),function(req,res){
      console.log(req.body);
      console.log(req.file);
      model1(req.file.path).save(function(err,data){
          if(err) throw err
          res.json(data);
      })
  });
  
  app.listen(80);
  console.log('Application listening on port 80....');
  </code></pre>
  
  <p>And here is my view (.ejs) for the form:</p>
  
  <pre><code>&lt;body&gt;
      &lt;% include partials/navigation.ejs %&gt;
      &lt;h1&gt;Adding an item to the shop&lt;/h1&gt;
      &lt;form id="theform" action="add_item" method="POST" enctype="multipart/form-data"&gt;
          Name of item:&lt;input type="text" name="item" class="thedata"&gt;&lt;br&gt;
          &lt;br&gt;
          Photo of item:&lt;input type="file" name="uploaded_file" class="thefile"&gt;
          &lt;button type="submit"&gt;Add item&lt;/button&gt;
      &lt;/form&gt;   
  &lt;/body&gt;
  </code></pre>