Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

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

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.

about 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda