0
votes

While connecting to mongodb, I am getting a warning

"DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect."

while submitting the form - I am getting

The database connection must be open to store files at GridFSStorage._handleFile (C:\Users\charan puli\Desktop\upload\node_modules\multer-gridfs-storage\lib\gridfs.js:341:17) at C:\Users\charan puli\Desktop\upload\node_modules\multer\lib\make-middleware.js:144:17 at allowAll (C:\Users\charan puli\Desktop\upload\node_modules\multer\index.js:8:3) at wrappedFileFilter (C:\Users\charan puli\Desktop\upload\node_modules\multer\index.js:44:7) at Busboy. (C:\Users\charan puli\Desktop\upload\node_modules\multer\lib\make-middleware.js:114:7) at Busboy.emit (events.js:182:13)

//////////////////app.js///////////////////////

//middle wares

app.use(bodyparser.json())

app.use(methodOverride('_method'))


app.set("view engine","ejs")

//connection

var mongoURI='mongodb+srv://user:[email protected]/test?retryWrites=true'


mongoose.connect(mongoURI,{useNewUrlParser:true})
        .then(()=>{console.log('connected successfully');
        })
        .catch(err=>{console.log(err);
        })

var conn=mongoose.connection
var gfs
conn.once('open',() =>{
gfs = Grid(conn.db, mongoose.mongo);

  gfs.collection('questions')
})
//create storage object

var storage = new GridFsStorage({
    url: mongoURI,
    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: 'questions'
          };
          resolve(fileInfo);
        });
      });
    }
  });
  const upload = multer({ storage });
//@route /upload POST

app.post('/upload',upload.single('file'),(req,res)=>{
    res.json({'file':req.file})
})


 var port= 3000
app.get("/",(req,res)=>{
    res.render('index')
})

app.listen(port,()=>{
    console.log(`app is running at ${port}`);

})


////////////////////////////////////

expected - json file object, connected Successfully

actual - database connection must be open

2

2 Answers

1
votes

This should work for you, I have tested it:

//connection

const mongoURI = 'mongodb+srv://user:[email protected]/test?retryWrites=true';

const promise = mongoose.connect(mongoURI, { useNewUrlParser: true });

const conn = mongoose.connection;
let gfs;

conn.once('open',() => {
  gfs = Grid(conn, mongoose.mongo);
  gfs.collection('questions');
});

//create storage object
const storage = new GridFsStorage({
  db: promise,
  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: 'questions'
        };
        resolve(fileInfo);
      });
    });
  }
});
const upload = multer({ storage });

See this reference here for further information: Multer's GridFS storage engine

It has to do with the new url parser of the new mongo client of the Node.js MongoDB Driver API. See this reference: MongoClient

I had the same issue that's why I ended up on your question.

0
votes

//connection

var mongoURI='mongodb+srv://charanpuli:Charan@[email protected]/test?retryWrites=true'

var conn = mongoose.connection

MongoClient.connect(db, { useNewUrlParser: true })

    .then(()=>{console.log('connected successfully');
    })
    .catch(err=>{console.log(err);
    })

var gfs conn.once('open',() =>{ gfs = Grid(conn.db, mongoose.mongo);

gfs.collection('questions') })