我能够将文件上传到我的mongoDB上:
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));
});
}
我发现this questions有所帮助,但是我仍然不确定其中有几件事。
答案中建议的代码是这样的:
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);
});
“根”部分是什么?我什至在使用正确的东西吗?文件保存在fs.files
和fs.chunks
中。
我试图将代码转换为我的情况。这就是我得到的:
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!
})
}
但是现在我得到了:
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)
我正在使用此链接尝试使其正常运行https://github.com/aheckmann/gridfs-stream#accessing-file-metadata