我正在尝试创建一个从数据库接收流的吸气剂,
var mongoose = require('mongoose');
var gridfs = require('mongoose-gridfs');
var express = require('express');
var router = express.Router();
var fs = require('fs');
mongoose.connection.on('open', function(){
const { model: Attachment } = gridfs({
collection: 'attachments',
model: 'Attachment',
mongooseConnection: mongoose.connection
});
router.post('/', function(req,res){
const readStream = fs.createReadStream(req.body.path);
var split = req.body.path.split('/');
const options = ({ filename: split[split.length-1] , contentType: 'binary/octet-stream' });
Attachment.write(options, readStream, (error, file) => {
if(error)console.log(error);
res.json(file._id);
});
});
router.get('/', function(req,res){
Attachment.findById(req.body.id, (error, attachment) => {
if(error) console.log(error);
console.log(attachment);
});
Attachment.readById(req.body.id, (error, attachment) => {
if(error) console.log(error);
console.log(attachment);
});
res.json(200)
})
})
因此我能够写入数据库,但是由于某种原因,我无法通过readbyId方法检索文件,但是,我可以在同一对象上执行findById。
FindById的输出
{ aliases: [],
_id: 5c9cda6253479f4fd4473581,
length: 17474,
chunkSize: 261120,
uploadDate: 2019-03-28T14:29:54.684Z,
filename: 'testphoto.PNG',
md5: 'a7d86bd3ba3a889a4c99844fb4287fb8',
contentType: 'binary/octet-stream' }
从ReadById中退出
Error: FileNotFound: file 5c9cda6253479f4fd4473581 was not found
如您所见,可以使用FindById方法找到ID,但不能通过ReadById方法找到ID。在这里,您会找到我正在使用的软件包:
https://www.npmjs.com/package/mongoose-gridfs
如果您需要任何其他信息,请告诉我,我将尽快对其进行编辑。预先谢谢你。