Hola a todos, por favor, ¿alguien puede ayudarme a actualizar el usuario que recibe la imagen y actualizarla y luego mostrar al usuario con la imagen en la interfaz?
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) })Debe agregar la opción {new: true} , para que el documento devuelto tenga los últimos datos actualizados, y también envíe la respuesta http en la devolución de llamada de la operación de actualización
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); } ); });