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

181
Vistas
if statement not running which includes an async function

I have an async function inside an if/else statement. I just want to check whether there is a file in the request body before even using it otherwise it throws an error. Why is this happening? And is there anything better I could do to handle the problem ?

//save image to cloudinary
if (typeof(banner) !== undefined) {
  //this piece of code is always executing even if the image is not present in request or is undefined
  cloudinary.uploader.upload(banner,
    async function(error, result) {

      if (error) {
        return res.status(409).send("Something went wrong while uploading image")
        console.log(error)
      }
      article.banner = result.url
      const savedArticle = await article.save()
      return res.status(201).send(JSON.stringify({
        redirect_uri: "/articles/" + savedArticle.slug
      }))

    })
} else {
  return res.status(400).send("Missing file")
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Your outer if does nothing, because typeof banner will never be the value undefined (it could be the string "undefined" though).

typeof returns a string with the type name in it, so if (typeof banner !== 'undefined') should work.

However, most likely you don't even need typeof here - if (banner !== undefined) or even just if (banner) should work too in your case.

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