I was able to upload a file to my mongoDB doing this:
uploadFile(file, uid) {
var fs = require('fs');
mongoose.connect(config.db, {useNewUrlParser: true},).catch(e => console.log(e));
var conn = mongoose.connection;
grid.mongo = mongoose.mongo;
const gfs = grid(conn.db);
const writeStream = gfs.createWriteStream({
filename: file.filename,
});
fs.createReadStream(file.path).pipe(writeStream);
writeStream.on('close', file => {
const {name} = file;
console.log(`${name} written to the db`);
return Account.findByIdAndUpdate(uid, {'employer.logo': name}).then(r => console.log(r)).catch(e => console.log(e));
});
}
I found this questions, which helped a bit, but I am still not sure about a couple of things.
The code suggested in the answer is this:
var conn = mongoose.connection;
var gfs = Grid(conn.db, mongoose.mongo);
gfs.findOne({_id: req.params.ID, root: 'resume'}, function (err, file) {
if (err) {
return res.status(400).send(err);
}
else if (!file) {
return res.status(404).send('Error on the database looking for the file.');
}
res.set('Content-Type', file.contentType);
res.set('Content-Disposition', 'attachment; filename="' + file.filename + '"');
var readstream = gfs.createReadStream({
_id: req.params.ID,
root: 'resume'
});
readstream.on("error", function (err) {
res.end();
});
readstream.pipe(res);
});
What is the "root" part? Am I even using the correct thing? The files are saved in fs.files
and fs.chunks
.
I tried to convert the code to my case. This is what I got:
downloadFile: async (req, res) => {
var mongoose = require('mongoose');
var Grid = require('gridfs-stream');
mongoose.connect(config.db, {useNewUrlParser: true},).catch(e => console.log(e));
var conn = mongoose.connection;
conn.once('open', function () {
var gfs = Grid(conn.db, mongoose.mongo);
gfs.findOne({_id: req.params.logoId}, function (err, file) {
if (err) {
console.log(err);
return res.status(400).send(err);
}
else if (!file) {
console.log('no file');
return res.status(404).send('Error on the database looking for the file.');
}
res.set('Content-Type', file.contentType);
res.set('Content-Disposition', 'attachment; filename="' + file.filename + '"');
var readstream = gfs.createReadStream({
_id: req.params.ID,
});
readstream.on("error", function (err) {
console.log(err);
res.end();
});
readstream.pipe(res);
});
// all set!
})
}
But now I'm getting:
TypeError: cursor.nextObject is not a function
at /home/alex/Documents/Projects/ontario-job-portal/node_modules/gridfs-stream/lib/index.js:143:12
at handleCallback (/home/alex/Documents/Projects/ontario-job-portal/node_modules/mongoose/node_modules/mongodb/lib/utils.js:128:55)
at Collection.<anonymous> (/home/alex/Documents/Projects/ontario-job-portal/node_modules/mongoose/node_modules/mongodb/lib/collection.js:431:45)
at Collection.deprecated [as find] (/home/alex/Documents/Projects/ontario-job-portal/node_modules/mongoose/node_modules/mongodb/lib/utils.js:661:17)
at Grid.findOne (/home/alex/Documents/Projects/ontario-job-portal/node_modules/gridfs-stream/lib/index.js:140:14)
at NativeConnection.<anonymous> (/home/alex/Documents/Projects/ontario-job-portal/globals/functions.js:114:17)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:106:13)
at NativeConnection.emit (events.js:208:7)
at /home/alex/Documents/Projects/ontario-job-portal/node_modules/mongoose/lib/connection.js:567:13
at result (/home/alex/Documents/Projects/ontario-job-portal/node_modules/mongoose/node_modules/mongodb/lib/utils.js:414:17)
at executeCallback (/home/alex/Documents/Projects/ontario-job-portal/node_modules/mongoose/node_modules/mongodb/lib/utils.js:406:9)
at err (/home/alex/Documents/Projects/ontario-job-portal/node_modules/mongoose/node_modules/mongodb/lib/operations/mongo_client_ops.js:286:5)
at connectCallback (/home/alex/Documents/Projects/ontario-job-portal/node_modules/mongoose/node_modules/mongodb/lib/operations/mongo_client_ops.js:241:5)
at process.nextTick (/home/alex/Documents/Projects/ontario-job-portal/node_modules/mongoose/node_modules/mongodb/lib/operations/mongo_client_ops.js:463:7)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
I was using this link to try and make it work https://github.com/aheckmann/gridfs-stream#accessing-file-metadata