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

120
Vistas
JavaScript: Accessing object inside an array inside object using multer

Here is my multer function -

const storage = multer.diskStorage({
  destination: (req, file, callback) => {
    let type = req.params.type;
    let path = `./data/${type}`;
    fs.mkdirsSync(path);
    callback(null, path);
  },
  filename: (req, file, cb) => {
    const ext = mime.extension(file.mimetype);
    const random = Math.floor(Math.random() * (9999 - 1000 + 1)) + 1000;
    cb(null, `${Date.now()}-${random}.${ext}`);
  },
});

Here is the router - router.post("/", auth, multer({ storage }).fields([{ name: 'image', maxCount: 1 }, { name: 'video', maxCount: 1 }]), addMovie); Here is what I'm getting when I'm console.log(req.files) -

[Object: null prototype] {
  image: [
    {
      fieldname: 'image',
      originalname: '����� ����.png',
      encoding: '7bit',
      mimetype: 'image/png',
      destination: './data/undefined',
      filename: '1631293039713-7613.png',
      path: 'data\\undefined\\1631293039713-7613.png',
      size: 13133
    }
  ],
  video: [
    {
      fieldname: 'video',
      originalname: 'file_example_MP4_480_1_5MG.mp4',
      encoding: '7bit',
      mimetype: 'video/mp4',
      destination: './data/undefined',
      filename: '1631293039720-3601.mp4',
      path: 'data\\undefined\\1631293039720-3601.mp4',
      size: 1570024
    }
  ]
}

I'm sending an image and a video. I want to access the fields inside the objects. And I'm not sure how to do that.

Tried that - req.files.image[0] and that's the error - Property 'image' does not exist on type '{ [fieldname: string]: File[]; } | File[]'. Property 'image' does not exist on type 'File[]'.ts(2339)

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

0

You can try (req as any).files.image[0]. Its a bit of a hassle with req typings :(

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