Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!