I'm getting the expected image with this code but it does not display in my pug template, instead it displays in the browser devtools console (i hope that makes sense), and it appears that I'm trying to set headers twice. My code produces [ERR_HTTP_HEADERS_SENT] as shown below.
I've read through numerous StackOverflow questions on this, including this thorough breakdown of the issue:
Error: Can't set headers after they are sent to the client
and I think what is actually happening is that I'm not correctly handling a callback, but I'm not sure.
I've really struggled with this and could use a little help pointing out where I'm going wrong.
route.js:
router.get('/issue/filedisplay/:id', issue_controller.file_display);
controller.js:
// controller.js
exports.file_display = function(req, res) {
gfs.files.findOne(
{ _id: mongoose.Types.ObjectId(req.params.id) },
(err, file) => {
//res.contentType(file.contentType);
// Check if file
if (!file || file.length === 0) {
return res.status(404).json({
err: 'No file exists',
});
}
const readstream = gfs.createReadStream(file._id);
readstream.pipe(res);
readstream.on('close', () => {
res.render('filedisplay', {
title: 'Display Image',
file: file._id,
});
});
//return res.json(file);
//return res;
}
);
};
Pug Template:
//-filedisplay.pug
extends layout
block content
h1= title
p Display Image
div
img.image(src="/list/issue/filedisplay/" + file alt="")
The error:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:482:11)
at ServerResponse.header (/home/don/dev/sandbox/fileupload-ex/file-upload-gridfs/node_modules/express/lib/response.js:771:10)
at ServerResponse.send (/home/don/dev/sandbox/fileupload-ex/file-upload-gridfs/node_modules/express/lib/response.js:170:12)
at done (/home/don/dev/sandbox/fileupload-ex/file-upload-gridfs/node_modules/express/lib/response.js:1008:10)
at Object.exports.renderFile (/home/don/dev/sandbox/fileupload-ex/file-upload-gridfs/node_modules/pug/lib/index.js:421:12)
at View.exports.__express [as engine] (/home/don/dev/sandbox/fileupload-ex/file-upload-gridfs/node_modules/pug/lib/index.js:464:11)
at View.render (/home/don/dev/sandbox/fileupload-ex/file-upload-gridfs/node_modules/express/lib/view.js:135:8)
at tryRender (/home/don/dev/sandbox/fileupload-ex/file-upload-gridfs/node_modules/express/lib/application.js:640:10)
at Function.render (/home/don/dev/sandbox/fileupload-ex/file-upload-gridfs/node_modules/express/lib/application.js:592:3)
at ServerResponse.render (/home/don/dev/sandbox/fileupload-ex/file-upload-gridfs/node_modules/express/lib/response.js:1012:7)
at /home/don/dev/sandbox/fileupload-ex/file-upload-gridfs/app.js:82:7
at Layer.handle_error (/home/don/dev/sandbox/fileupload-ex/file-upload-gridfs/node_modules/express/lib/router/layer.js:71:5)
at trim_prefix (/home/don/dev/sandbox/fileupload-ex/file-upload-gridfs/node_modules/express/lib/router/index.js:315:13)
at /home/don/dev/sandbox/fileupload-ex/file-upload-gridfs/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/home/don/dev/sandbox/fileupload-ex/file-upload-gridfs/node_modules/express/lib/router/index.js:335:12)
at next (/home/don/dev/sandbox/fileupload-ex/file-upload-gridfs/node_modules/express/lib/router/index.js:275:10)