title: The database connection must be open to store files at GridFSStorage._handleFile
body: <p>I try to upload a file on local MongoDB server via a web application in the following:</p>
  
  <pre><code>const express = require("express");
  const bodyParser = require('body-parser');
  const path = require('path');
  const crypto = require('crypto');
  const mongoose = require('mongoose');
  const GridFsStorage = require('multer-gridfs-storage');
  const Grid = require('gridfs-stream');
  const methodOverride = require('method-override');
  const multer = require('multer');
  
  const app = express();
  
  // Middleware
  app.use(bodyParser.json());
  app.use(methodOverride('_method'));
  app.set('view engine', 'ejs');
  
  // Mongo URI
  const mongoURI =
      'mongodb://localhost:27017/database'
  
  // Create mongo connection
  const conn = mongoose.createConnection(mongoURI);
  
  // Init gifs
  let gfs;
  
  conn.once('open', () =&gt; {
      // Init Stream
      gfs = Grid(conn.db, mongoose.mongo);
      gfs.collection('uploads');
  })
  
  
  // Create storage engine
  
  const storage = new GridFsStorage({
    url: 'mongodb://localhost:27017/database',
    file: (req, file) =&gt; {
      return new Promise((resolve, reject) =&gt; {
        crypto.randomBytes(16, (err, buf) =&gt; {
          if (err) {
            return reject(err);
          }
          const filename = buf.toString('hex') + path.extname(file.originalname);
          const fileInfo = {
            filename: filename,
            bucketName: 'uploads'
          };
          resolve(fileInfo);
        });
      });
    }
  });
  const upload = multer({ storage });
  
  // @route GET /
  // @desc Loads form
  
  app.get('/', (req, res) =&gt; {
      res.render('index');
  })
  
  // @route POST /upload
  //@desc upload file to DB
  app.post('/upload', upload.single('file'), (req, res) =&gt; {
  res.json({ file: req.file });
  });
  
  const port = 27017;
  </code></pre>
  
  <blockquote>
    <p>app.listen(port, () => console.log('server started on port ${port}'));
    but I get error: "The database connection must be open to store files
        at GridFSStorage._handleFile (/Users/payam/Desktop/mongodb-uploads/node_modules/multer-gridfs-storage/lib/gridfs.js:341:17)
        at /Users/payam/Desktop/mongodb-uploads/node_modules/multer/lib/make-middleware.js:144:17
        at allowAll (/Users/.../Desktop/mongodb-uploads/node_modules/multer/index.js:8:3)
        at wrappedFileFilter (/Users/.../Desktop/mongodb-uploads/node_modules/multer/index.js:44:7)
        at Busboy. (/Users/.../Desktop/mongodb-uploads/node_modules/multer/lib/make-middleware.js:114:7)
        at Busboy.emit (events.js:182:13)
        at Busboy.emit (/Users/.../Desktop/mongodb-uploads/node_modules/busboy/lib/main.js:38:33)
        at PartStream. (/Users/.../Desktop/mongodb-uploads/node_modules/busboy/lib/types/multipart.js:213:13)
        at PartStream.emit (events.js:182:13)
        at HeaderParser. (/Users/.../Desktop/mongodb-uploads/node_modules/dicer/lib/Dicer.js:51:16)
        at HeaderParser.emit (events.js:182:13)
        at HeaderParser._finish (/Users/.../Desktop/mongodb-uploads/node_modules/dicer/lib/HeaderParser.js:68:8)
        at SBMH. (/Users/.../Desktop/mongodb-uploads/node_modules/dicer/lib/HeaderParser.js:40:12)
        at SBMH.emit (events.js:182:13)
        at SBMH._sbmh_feed (/Users/.../Desktop/mongodb-uploads/node_modules/streamsearch/lib/sbmh.js:159:14)
        at SBMH.push (/Users/.../Desktop/mongodb-uploads/node_modules/streamsearch/lib/sbmh.js:56:14)".
    can Any body help and tell me what is wrong with my code or with my
    machine?</p>
  </blockquote>