OK so I found a fix for this problem.
Task : Upload Image on mongoDB as binary data, using buckets, chunks etc.
Reference URL
Code:
const express = require("express");
const router = express.Router();
const User = require('../models/user');
const grid = require('gridfs-stream');
const GridFsStorage = require('multer-gridfs-storage');
const util = require("util");
const crypto = require('crypto');
const path = require('path');
const methodOverride = require('method-override');
const bodyParser = require('body-parser');
const dotenv = require("dotenv").config({path: "./config/config.env"});
const multer = require('multer');
const storage = new GridFsStorage({
url: process.env.MONGO_URI,
options: {
useUnifiedTopology: true
},
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);
});
});
}
});
var uploadFile = multer({storage: storage}).single("file");
var uploadFilesMiddleware = util.promisify(uploadFile);
module.exports = uploadFilesMiddleware;
Problem:
Deprecation Warning Listening to events on the Db class has been
deprecated and will be removed in the next major version.
Fix:
So most probably the problem is not because of you. It is in the package itself. (check out the reference link above)
Go to node_modules (it is that one folder with a lot of files)
folder in your node_js application.
Go to multer-gridfs-storage (inside node_modules)
Go to lib folder (inside multer-gridfs-storage)
Open gridfs.js
Find this comment (// This are all the events that emit errors)
Replace This
this.db
.on('error', errEvent)
.on('parseError', errEvent)
.on('timeout', errEvent)
.on('close', errEvent);
With this
this.client
.on('error', errEvent)
.on('parseError', errEvent)
.on('timeout', errEvent)
.on('close', errEvent);
Basically change replace 'db' with 'client'.
You can also go to the official page and check out the current issues there it is clearly mentioned that multer-gridfs-storage has debrication waring issue.
Issue Link