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

128
Vistas
Creating an object only the first time in mongodb

I'm making an app where a user makes a safe by going to /mysafe. But i only want to create it the first time when he goes there and for the next time when he goes to /mysafe it should redirect to /mysafe/theidofit.

See my app.js

app.get('/mysafe',async (req,res)=> {
    const safe=new Safe()
    safe.author=req.user._id
    safe.save()
    res.redirect(`/mysafe/${safe._id}`)
})

I tried adding a middleware but in my user schema i dont have a safe defined.Do i need to define it or can i do it without it?

const UserSchema = new Schema({
    email: {
        type: String,
        required: true,
        unique: true
    }   
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

app.get('/mysafe', async (req,res)=> {
  let safe = await Safe.findOne({author: req.user._id});
  if (!safe) {
    safe = new Safe()
    safe.author=req.user._id
    safe.save()
  }
  res.redirect(`/mysafe/${safe._id}`)
})
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