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

237
Vistas
Fill row with data MongoDB

I'm using MongoDB and I want to fill the row 'userImg' that is in my User model. Below is my code:

User model:

var mongoose = require("mongoose");
var Schema = mongoose.Schema;

(userSchema = new Schema(
  {
    unique_id: Number,
    email: String,
    username: String,
    ageRange: String,
    phone: String,
    hobby: String,
    fact: String,
    password: String,
    passwordConf: String,
    userImg: String,
  },
  {
    timestamps: true,
  }
)),
  (User = mongoose.model("User", userSchema));

module.exports = User;

Index.js (where I want to fill the userImg row of the User model):

var storage = multer.diskStorage({
  destination: function(req, file, cb) {
    cb(null, "file/");
  },
  filename: function(req, file, cb) {
    cb(null, file.originalname);
  },
});

var upload = multer({ storage: storage });
router.post("/upload", upload.single("file"), function(req, res, next) {
  if (req.file === undefined) return res.send("you must select a file.");
  const imgUrl = `http://localhost:3000/file/${req.file.originalname}`;
  var userId = req.query.id;
  console.log(userId);
  const bla = User.findOne({ unique_id: userId }).populate({ path: "userImg", model: "User" })
  bla.userImg = imgUrl;
  return res.send("Uw afbeelding is succesvol geupload! " + bla.userImg);
});

However, when executing this request, the row userImg is not updated in MongoDB: enter image description here

In this row I would like to put a URL of the image uploaded by the user.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You should use the findOneAndUpdate method to update the userImg property.
Also, your should use async await since it returns a Promise.

Try to change your code like this:

router.post("/upload", upload.single("file"), async function(req, res, next) {
    if (req.file === undefined) return res.send("you must select a file.");
    const imgUrl = `http://localhost:3000/file/${req.file.originalname}`;
    var userId = req.query.id;
    const bla = await User.findOneAndUpdate(
        { unique_id: userId },
        { userImg: imgUrl },
        { new: true }
      );
    return res.send("Uw afbeelding is succesvol geupload! " + bla.userImg);
  });
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