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

156
Vistas
async/await and image upload

I want to create a post and upload an image if it is provided.

If I do upload the image everything works fine but if I don't I get this error:

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'image' of null

I understand it's to do with async/await but I do not know how to rewrite it or that the problem is specifically?

app.post("/posts/create", async (req, res) => {
    let image = req.files.image; 

    if (image !== null) {
        image.mv(
            path.resolve(__dirname, "public/images", image.name) 
        );
    }

    await Post.create(req.body);
    res.redirect("/");
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

That's not because async/await, it is simply because you don't have any .files in your request, so you just need a middleware for handling multipart/form-data like multer, and then pass the middleware through the app.post so you can access to req.files, if you want to upload that image to a server like cloudinary you can just use multer-cloudinary-storage.

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