将节点从 v12 升级到 v14 后,库 skipper-gridfs 停止正常工作。有人遇到同样的问题吗?
上传文件似乎可行,即使触发了Done()并创建了文件描述符,但是当我需要检索文件时它不存在,并给出以下错误:
Error: FileNotFound: file b33b78d2-30ea-4fe3-9228-2a8a7ccc8a77.PNG was not found
at /home/melich/Documents/becquel_backend/node_modules/skipper-gridfs/node_modules/mongodb/lib/gridfs-stream/download.js:284:17
at executeCallback (/home/melich/Documents/becquel_backend/node_modules/skipper-gridfs/node_modules/mongodb/lib/operations/execute_operation.js:70:5)
at handleCallback (/home/melich/Documents/becquel_backend/node_modules/skipper-gridfs/node_modules/mongodb/lib/utils.js:128:55)
是由节点 v14 引起的,如果我转向 v12 它可以工作,但我不能使用它,因为它必须与 Angular9 实例共存。
所以我附上了我用来上传和下载的代码,以防万一,但它没有任何秘密,因为它在节点 v12 上工作了几个月:
下载:
download: async function (req, res) {
var blobAdapter = require('skipper-gridfs')({
uri: 'mongodb://root:pASSWORD@127.0.0.1/files'
});
var fileID = req.param('id');
const fileModel = await Files.findOne({id: fileID});
blobAdapter.read(fileModel.fd, function(error , file) {
if(error) {
console.log(error);
res.json(error);
} else {
res.contentType(fileModel.contentType);
res.send(new Buffer(file));
}
});
}
上传:
uploadFile: function (req, res) {
const budgetID = req.param('id');
const scope = req.param('scope');
req.file('file').upload({
// don't allow the total upload size to exceed ~10MB
maxBytes: 2 * 10000000,
adapter: require('skipper-gridfs'),
uri: 'mongodb://root:pASSWORD@127.0.0.1/files'
}, async function whenDone(err, uploadedFiles) {
if (err) {
return res.serverError(err);
}
// If no files were uploaded, respond with an error.
if (uploadedFiles.length === 0){
return res.badRequest('No file was uploaded');
}
var file = await Files.create({
fd: uploadedFiles[0].fd,
name: uploadedFiles[0].filename,
contentType: uploadedFiles[0].type,
scope: scope,
groupID: req.session.userData.group,
userID: req.session.userData.id
}).fetch();
await Budget.addToCollection(budgetID, 'files').members([file.id]);
return res.ok(file);
});
} };