我正在使用带有节点和mongo后端的React应用程序。 mongo后端将使用Gridfs-stream存储图像。我已经成功实现了图像保存,但是现在无法从数据库中提取特定图像。
gfs.files.findOne的功能正常运行,直到将其移至单独的文件并将其添加到模块导出中为止。我不知道是什么原因导致执行失败。
在database.js中获取图像:
exports.getFile = (filename) => {
console.log('loading file: ' + filename);
gfs.files.findOne({ filename : filename }, (err, file) => {
console.log('finding file');
if (file) {
// checking specifically for image here
if (file.contentType == 'image/png' || file.contentType == 'image/jpeg') {
res.set('Content-Type', file.mimetype);
const readstream = gfs.createReadStream(file.filename);
console.log('returning readstream');
return readstream;
}
else {
console.log('error loading');
const err = { "status": "error", "details": "File is not an image" };
return err;
}
}
else {
console.log('error loading');
const err = { "status": "error", "details": "Failed to find specified file" };
return err;
}
});
console.log('function ending');
}
在api中调用函数路由users.js:
let rs = database.getFile(pic.profilePicture);
if (rs.status && rs.status === "error") {
res.json(rs);
}
else {
// we have a readstream
rs.pipe(res);
}
控制台输出在终端:
[0] testing retrieving profile picture
[0] loading file: 7be7c6a99a96023763b753febd85a92e.png
[0] function ending
[0] events.js:173
[0] throw er; // Unhandled 'error' event
[0] ^
[0]
[0] TypeError: Cannot read property 'status' of undefined
[0] at User.getProfilePicture (/Users/zachdobbs/Documents/Projects/Better/routes/api/users.js:44:20)
[0] at /Users/zachdobbs/Documents/Projects/Better/node_modules/mongoose/lib/model.js:4698:16
[0] at /Users/zachdobbs/Documents/Projects/Better/node_modules/mongoose/lib/query.js:4099:12
[0] at process.nextTick (/Users/zachdobbs/Documents/Projects/Better/node_modules/mongoose/lib/query.js:2681:28)
[0] at processTicksAndRejections (internal/process/next_tick.js:74:9)
[0] Emitted 'error' event at:
[0] at /Users/zachdobbs/Documents/Projects/Better/node_modules/mongoose/lib/model.js:4700:13
[0] at /Users/zachdobbs/Documents/Projects/Better/node_modules/mongoose/lib/query.js:4099:12
[0] at process.nextTick (/Users/zachdobbs/Documents/Projects/Better/node_modules/mongoose/lib/query.js:2681:28)
[0] at processTicksAndRejections (internal/process/next_tick.js:74:9)
[0] [nodemon] app crashed - waiting for file changes before starting...
据我所见,gfs.files.findOne函数正在跳过整个块,并直接转到函数的末尾。显示的唯一日志是loading file: ...png
和function ending
输出。这些日志位于函数的开头和结尾,但是函数内的其他日志均未执行。我不知道为什么会这样。如果有人可以帮助我解决这个问题,将不胜感激。
答案 0 :(得分:0)
赞:
exports.getFile = (filename) => {
console.log('loading file: ' + filename);
gfs.findOne({ filename : filename }, (err, file) => {
console.log('finding file');
if (file) {
// checking specifically for image here
if (file.contentType == 'image/png' || file.contentType == 'image/jpeg') {
res.set('Content-Type', file.mimetype);
const readstream = gfs.createReadStream(file.filename);
console.log('returning readstream');
return readstream;
}
else {
console.log('error loading');
const error = { status: "error", details: "File is not an image" };
return error;
}
}
else {
console.log('error loading');
const error = { status: "error", details: "Failed to find specified file" + err };
return error;
}
});
console.log('function ending');
}
然后:
let rs = database.getFile(pic.profilePicture);
if (rs.status && rs.status === "error") {
res.json(JSON.stringify(rs));
}
else {
// we have a readstream
rs.pipe(res);
}