Model with unique attribute is being created twice

131 views Asked by At

I am using this passport-generate-auth module, and I am trying to get my grasp around understanding this whole thing.

So, in my User model, I've got

var User = {
    schema: true,

    attributes: {
        username: {
            type: 'string',
            unique: true,
            required: true
        },

        email: {
            type: 'email',
            unique: true,
            required: true
        },

        password: {
            type: 'string',
            required: true,
            minLength: 8
        },
    }
};

module.exports = User;

And when I call

exports.register = function (req, res, next) {
    var email = req.param('email')
        , username = req.param('username')
        , password = req.param('password');

    User.create({
            username: username
            , email: email
            , password: password
        }, function (err, user) {
            if (err) {
                if (err.code === 'E_VALIDATION') {
                    if (err.invalidAttributes.email) {
                        req.flash('error', 'Error.Passport.Email.Exists');
                    } else {
                        req.flash('error', 'Error.Passport.User.Exists');
                    }
                }

                return next(err);
            }
        });
    };
};

when providing username and email that already exist in the database, the new entry is stored in the DB, instead of giving me an error msg.

Isn't User.create() supposed to take care of checking in the schema attributes rules whether they are unique and then check the records in the DB for a record with a value that already exists?

0

There are 0 answers