gridfs-stream icon indicating copy to clipboard operation
gridfs-stream copied to clipboard

Got DeprecationWarning from mongoose

Open changrui0608 opened this issue 8 years ago • 2 comments
trafficstars

The file saving was successful, only if this not appear in my node console...

(node:27019) DeprecationWarning: `ensureIndex()` is deprecated in Mongoose >= 4.12.0, use `createIndex()` instead

I did not see this warning before add gridfs-stream and write code on GridFS I/O, and the warning happened at time when I write file to GridFS, so I think this may related to gridfs-stream.

This is my code if needed. The code is a kind of verbose, but acutally did not do anything special.

// app.js, the entrance
import mongoose from 'mongoose'
import Grid from 'gridfs-stream'

mongoose.Promise = Promise // use ES6 Promise
mongoose.connect(config.get('mongo'), { useMongoClient: true })
mongoose.connection.on('error', function (err) {
  console.error(err)
  process.exit(1)
})
Grid.mongo = mongoose.mongo
// ...
// router file, with express and multer
router.post('/', upload.single('image'), function (req, res, next) {
  const file = req.file
  const metadata = req.body['metadata']
  console.log(file) // { fieldname, originalname, encoding, mimetype, size, buffer }
  // the warning shows after here, before `writeStream on close`
  const writeRes = image.write(file.originalname, file.buffer, metadata)
  res.json({ imageId: writeRes.id })
})
// function I wrapped
export function write (filename, filedata, metadata) {
  const gfs = Grid(mongoose.connection.db, mongoose.mongo)
  const id = generateId()
  const options = {
    id: id,
    mode: 'w',
    filename: filename,
    root: root,
    metadata: metadata
  }
  const writeStream = gfs.createWriteStream(options)
  const fileStream = buffer2stream(filedata)

  writeStream.on('close', function (file) {
    console.log('done')
    console.log(file)
  })

  fileStream.pipe(writeStream)
  return { id: id }
}

changrui0608 avatar Oct 18 '17 11:10 changrui0608

+1

kkys4bfgp75be9p avatar Nov 14 '17 17:11 kkys4bfgp75be9p

I'm also having this issue. It's probably because mongoose dropped support for mongodb driver versions < 2.0.36. I see this project uses [email protected] so updating to a more recent version of mongoose should fix it @aheckmann

e-oj avatar Dec 31 '17 04:12 e-oj