Saturday, 29 April 2017

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

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?



via Behrouz Riahi

No comments:

Post a Comment