I try to upload a file on local MongoDB server via a web application in the following:
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', () => {
// 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) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
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) => {
res.render('index');
})
// @route POST /upload
//@desc upload file to DB
app.post('/upload', upload.single('file'), (req, res) => {
res.json({ file: req.file });
});
const port = 27017;
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?