-1

I am trying to sign up a new user via Angular app, but when I am registering a user with the same username, Mongo allows me to do this, so it does not reurn an error and afterwards I can see two users with same names in the db. But I do mark name field as unique.

part of api:

apiRoutes.post('/signup', function(req, res) {
    if (!req.body.userId || !req.body.password) {
        res.json({success: false, msg: 'Please pass name and password.'});
    } else {
        var newUser = new User({
            name: req.body.name,
            password: req.body.password,
            wallet: req.body.wallet,
            userPic: req.body.userPic
        });
        // save the user
        newUser.save(function(err) {
            if (err) {
                return res.json({success: false, msg: 'Username already exists.'});
            }
            res.json({success: true, msg: 'Successful created new user.'});
        });
    }
});

model code:

// set up a mongoose model
var UserSchema = new Schema({
    name: {
        type: String,
        unique: true,
        required: true
    },
    password: {
        type: String,
        required: true
    },

    wallet: {
        type: Number,
        required: true
    },

    userPic: {
        type: String,
        required: true,
        unique: true
    }

    });

And POST request code(login and password are taken from outside):

 let newUser = {
      password: password,
      wallet: 0,
      userPic: md5(login),
      name: login
    };   
    this.$http.post('http://127.0.0.1:8080/api' + '/signup', newUser);
boooni
  • 153
  • 1
  • 3
  • 9

1 Answers1

-1

try in your model:

name: {
        type: String,
        index:{unique: true},
        required: true
    }
adkirvien
  • 44
  • 3