title: How to save uploaded image from multer to mongodb in nodejs
body: <p>I need to save user information from a register form to mongodb. 
  Everything works well except the image file. I am using Multer for uploading images from form to server. </p>
  
  <p>Also to show that image in the profile page once the user logged in.</p>
  
  <p>Below is the code:</p>
  
  <p>signup.ejs:</p>
  
  <pre><code>&lt;form action="/signup" method="post" enctype="multipart/form-data"&gt;
      &lt;label for&gt;Profile Picture&lt;/label&gt;
      &lt;input type="file" name="image"&gt;&lt;br&gt;&lt;br&gt;
      &lt;label for&gt;Name&lt;/label&gt;              
      &lt;input type="text" name="name"&gt;&lt;br&gt;&lt;br&gt;
      &lt;label for&gt;Email&lt;/label&gt;              
      &lt;input type="email" name="email"&gt;&lt;br&gt;&lt;br&gt;
      &lt;label for&gt;Password&lt;/label&gt;  
  &lt;/form&gt; 
  </code></pre>
  
  <p>routes.js:</p>
  
  <pre><code>var multer       = require('multer');
  
  var storage = multer.diskStorage({
     destination: function (req, file, cb) {
     cb(null, './uploads');
  },
  filename: function (req, file, cb) {
     cb(null, file.originalname);
  }
  });
  
  var upload = multer({ storage: storage });
  
  module.exports = function(app, passport) {
  
  app.post('/signup', upload.single('image'), passport.authenticate('local-signup', {
      successRedirect : '/login', 
      failureRedirect : '/',
      failureFlash : true 
  }));
  </code></pre>
  
  <p>The image is uploaded in /uploads folder.</p>
  
  <p>But how to get that image and save it in mongodb. I am using passport.js and below is the code for saving post data.</p>
  
  <p>UserModel.js:</p>
  
  <pre><code>var mongoose = require('mongoose');
  var Schema = mongoose.Schema;
  var userSchema = new Schema({
    image: { 
        data        : Buffer, 
        contentType : String 
    },
    name: {
        type: String,
    },
    email: {
        type: String,
    },
    password: {
        type: String,
    }
  });
  </code></pre>
  
  <p>passport.js:</p>
  
  <pre><code>passport.use('local-signup', new LocalStrategy({
      usernameField : 'email',
      passwordField : 'password',
      passReqToCallback : true
  },
  function(req, email, password, done) {
      User.findOne({ 'email' :  email }, function(err, user) {
          if (err) {
              //errorLogger.error(err);
              return done(err);
          }
  
          if (user) {
              return done(null, false, req.flash('signupMessage', 'Email already exists.'));
          } else {
              var newUser = new User();
              //newUser.image = "dont know how to get image from /uploads" 
                newUser.name = req.body.name;
                newUser.email = req.body.email;
                newUser.password = req.body.password;
  
                newUser.save(function(err) {
                  if (err)
                      throw err;
                  return done(null, newUser, req.flash('signupMessage', 'User created'));
              });
          }
  });
  </code></pre>