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

152
Vistas
My schema doesn't function as expected when inserting the data

here is my Schema code

var Schema       = mongoose.Schema;

var UserSchema   = new mongoose.Schema({
    username: {
        type: String,
        required: true,
        unique: true
      },
    phone:{
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    createdAt: {type: Date, default: Date.now}
});

and I am inserting the data as it is shown below (inside another file)

router.route('/register')
.post(function(req, res) {
    var registeration = new Registeration({
        username: req.body.username,
        phone: req.body.phone,
        password: req.body.password
    });

    registeration.save(function(err) {
        if (err){
            return res.json({ success: false, message: 'That username already exists.'});
        }
        res.json({ success: true, message: 'Successfully created new user.' });
    })
});

The problem here is that, whenever I register new account it is successfully added even if the username already exists in my database

I set the attribute to unique: true but it doesn't seem to work for some reason.

What am I doing wrong here?

about 4 years ago · Santiago Trujillo
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