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

136
Vistas
How to check if an object has been already created?

Hello i would like to check if an object was created. If its created then redirect to the id page but if it isnt created redirect to the page where its being created.

Here is my app

app.get('/my',isLoggedIn,async (req,res)=> {
    const safe= await Safe.find({});
    if(safe===undefined){
        res.redirect('/mysafe')
    }
    else {
         res.redirect(`mysafe/${safe._id}`)
    }
})

i think it should work but im getting a cast error CastError: Cast to ObjectId failed for value "undefined" (type string) at path "_id" for model "Safe"

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

0

You just need to flip your conditional and use if(safe) as the condition. That will be true if the object has been created and false if not. Something along the lines of:

app.get('/my', isLoggedIn, async (req,res) => {
    const safe= await Safe.find({});
    if(safe){
        res.redirect(`mysafe/${safe._id}`)
    } else {
        res.redirect('/mysafe')
    }
})

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