Monday, 24 April 2017

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

I upload 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.

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 iamge as well as name and email

help me out



via its me

No comments:

Post a Comment