问题
I uploaded files using multer and gridfs storage and everything is working fine. After uploading file I saved my userdetails with filepath in user collection.
Now I want to retrieve userdata from user collection.
In the response, I need to send image and user details.
app.get('/user', function(req, res){
User.findById('58fdea1793e794256c2e0820', function (err, user) {
if(err) { return handleError(res, err); }
if(!user) { return res.status(404).send('Not Found'); }
//console.log(user);
var readstream = gfs.createReadStream({
filename: user.filepath,
root: "ctFiles"
});
/** set the proper content type */
res.set('Content-Type', user.filepath.contentType)
//return readstream.pipe(res);
return res.json(user);
});
For this user I am getting response like this
{
_id: 58fdea1793e794256c2e0820,
name: 'asdfs',
email:'asdf@gmail.com'
filepath: 'file-1493035543005.jpg',
__v: 0
}
If I use 'return readstream.pipe(res);' image got displayed but I want to send image as well as name and email
Help me out.
来源:https://stackoverflow.com/questions/43603303/how-to-send-json-data-with-file-in-same-response-using-express-node-mongodb