I'm trying to have a play with gridfs-stream but I'm having issues posting an image file to my upload controller. When it tried to create the write stream is returns grid.mongo.ObjectID is not a constructor at new GridWriteStream
I've searched around and not really found anything that helps me with this issue, and debugging hasn't turned anything up I can work with, this is all new technologies for me.
Here's my code:
const router = require('express').Router();
const database = require('../config/database');
var mongoose = require('mongoose');
var Grid = require('gridfs-stream');
Grid.mongo = mongoose.connection;
var gfs = new Grid("db", mongoose.mongo.db);
router.get('/', function(req, res) {
res.send("Greetings");
});
router.post('/img', function(req, res) {
var part = req.files.fileField;
var writeStream = gfs.createWriteStream({
filename: part.name,
mode: 'w',
content_type:part.mimetype
});
writeStream.on('close', function() {
return res.status(200).send({
message: 'Success'
});
writeStream.end();
});
});
module.exports = router;
I have also tried passing just mongoose.mongo
to the gfs object as the second argument but that also threw an error about being able to read readPreferences of undefined