Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda