How to send json data with file in same response using express,node,mongodb?

两盒软妹~` 提交于 2020-01-17 07:21:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!