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

163
Vistas
request sent me empty object

Hi every one please can someone help me to update user that receive image and updated it then render the user with the image to frontend.i get stuck I search by google but I didn't find what I search for,there's my code :

this is the userModel:
import mongoose from "mongoose";



const userSchema = new mongoose.Schema({
    
    name: {type: String, required: true},
    fname: {type: String, required: true},
    phone: {type: String, required: true},
    email: {type: String, required: true, unique: true},
    password: {type: String, required: false},
    image: {type: {name: String, img: {contentType: String, data: Buffer}}, required: false},
    isAdmin: {type: Boolean, default: false, required: true},
    }, {
        timeStamps: true
    });
    const User = mongoose.model("User", userSchema);
    export defau
userRouter.post("/upload/:id", async(req, res)=> {
    upload.single("productImage")
    
     const updatedUser = User.findByIdAndUpdate(req.params.id,{$set: {image: {name: req.files.productImage.name, img: {contentType: req.files.productImage.mimetype, data: fs.createReadStream(path.join(__dirname, '../../frontend/public/images'))}}}   
       })
    
  res.send("ok");
  console.log(updatedUser)
})
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You have to add option {new: true}, so that the document returned has the last updated data, and also send the http response in the callback of the update operation

userRouter.post('/upload/:id', async (req, res) => {
  upload.single('productImage');

  const updatedUser = User.findByIdAndUpdate(
    req.params.id,
    {
      $set: {
        image: {
          name: req.files.productImage.name,
          img: {
            contentType: req.files.productImage.mimetype,
            data: fs.createReadStream(
              path.join(__dirname, '../../../frontend/public/images')
            ),
          },
        },
      },
    },
    { new: true },
    (err, doc) => {
      if (err) return res.send(err);

      res.send(doc);
    }
  );
});
about 4 years ago · Juan Pablo Isaza Denunciar
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