Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

150
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda