我正在尝试从GridFS读取图像。并得到以下错误。
var mongoose = require('mongoose');
exports.getLogo = function (req, res, next) {
var conn = mongoose.createConnection('localhost', 'database-name', 27017);
conn.once('open', function () {
var gs = new mongoose.GridStore(conn.db, 'img1.png', "w");
gs.read(function (err, data) {
res.setHeader('Content-Type', gs.contentType);
res.setHeader('Content-Length', gs.length);
res.setHeader('Content-Disposition', 'inline; filename="' + gs.filename + '"');
res.end(data);
});
});
};
错误: -
D:\Projects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\db.js:279
throw err;
^
TypeError: undefined is not a function
如何解决此错误。
已编辑 -
TypeError: Cannot call method 'length' of undefined
at Stream.GridStore.read
gridfs-stream输出: -
答案 0 :(得分:2)
你真的需要仔细阅读这份文件。
var mongoose = require('mongoose');
exports.getLogo = function (req, res, next) {
var conn = mongoose.createConnection('localhost', 'database-name', 27017);
conn.once('open', function () {
var gs = new mongoose.mongo.GridStore(conn.db, /* the id */, 'img1.png', "r");
gs.open(function(err, gs) {
gs.read(gs.length, function (err, data) {
res.setHeader('Content-Type', gs.contentType);
res.setHeader('Content-Length', gs.length);
// gs don't have a property filename, filename need to come from your req.
res.setHeader('Content-Disposition', 'inline; filename="' + 'img1.png' + '"');
res.end(data);
});
})
});
};