gpt4 book ai didi

mongodb - 从 MongoDB 下载图像文件

转载 作者:可可西里 更新时间:2023-11-01 10:40:19 26 4
gpt4 key购买 nike

尝试使用路由从 MongoDB 下载图像文件:

router.get('/:id', function(req, res) {
gfs.findOne({ _id: mongoose.Types.ObjectId(req.params.id) }, function(err, file) {
if (err) {
common.handleError(err, res);
return;
}

if (!file) {
return res.json({ status: constants._ERROR, body: { message: constants._FILE_NOT_FOUND }});
}

res.set('Content-Type', file.contentType);
res.set('Content-Disposition', 'attachment; filename="' + file.filename + '"');

//console.log(file);
var readstream = gfs.createReadStream({
_id: file._id
});

readstream.on('error', function (err) {
console.error('Read error', err);
res.end();
});

readstream.pipe(res);
});
});

堆栈跟踪:

/Users/eric.miles/Projects/Personal/mean/emcontacts/node_modules/mongodb/lib/utils.js:123
process.nextTick(function() { throw err; });
^

TypeError: Cannot read property 'readPreference' of null
at new GridStore (/Users/eric.miles/Projects/Personal/mean/emcontacts/node_modules/mongodb/lib/gridfs/grid_store.js:130:66)
at new GridReadStream (/Users/eric.miles/Projects/Personal/mean/emcontacts/node_modules/gridfs-stream/lib/readstream.js:68:17)
at Grid.createReadStream (/Users/eric.miles/Projects/Personal/mean/emcontacts/node_modules/gridfs-stream/lib/index.js:53:10)
at /Users/eric.miles/Projects/Personal/mean/emcontacts/server/routes/file.js:51:26

版本:

"gridfs-stream": "^1.1.1",
"mongoose": "^4.11.13"

在 createReadStream 行上搞砸了。文件肯定在数据库中,“fs.files & fs.chunks”。

暂时尝试通过 Postman 访问。

最佳答案

所以,我必须做的就是像这样设置我的数据库连接:

mongoose.connect(mongodb_url, { useMongoClient: true });
var db = mongoose.connection;
db.options = {};
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.info('database connection successful');
});

“db.options = {};”是踢球者。

关于mongodb - 从 MongoDB 下载图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46413947/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com