ValidatingError: MongooseError AND MongoError E11000 #4883

Open
laerkeli opened this Issue Jan 9, 2017 · 6 comments

Projects

None yet

3 participants

@laerkeli
laerkeli commented Jan 9, 2017 edited

UPDATE AGAIN.. Actually made a user, but when I create a new one, the error 11000 comes again.
UPDATE: I've deleted my users: and then THIS came....
skaermbillede 2017-01-09 kl 22 56 13
"express": "^4.14.0",
"mongodb": "^2.2.19",
"mongoose": "^4.7.6"
I've stared using Mean stack in a school project, and follow the guide from udemy, Mean stack zero to hero. My problem is when I'm trying to register a user, then this Mongo Error: 11000 comes up.. I've looked through the code again and again, and I can't seem to find the error.
skaermbillede 2017-01-09 kl 17 04 52

This is my model:
var mongoose = require('mongoose');

var userSchema = new mongoose.Schema({
username: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
}
});
mongoose.model('User', userSchema);

And this i the controller for register:

angular.module('roskildeapp').controller('RegisterController', RegisterController);

function RegisterController($http) { //dolla build in angular
var vm = this;

vm.register = function() {
    var user = {
        username: vm.username,
        password: vm.password
    };

    if (!vm.username || !vm.password) { //if user dosent exist //
        vm.error = 'Tilføj et brugernavn og kodeord.';
    } else {
        if (vm.password !== vm.passwordRepeat) {
            vm.error = 'Kodeordene skal være ens.';
        } else {
            $http.post('api/users/register', user).then(function(result) {
                console.log(result);
                vm.message = 'Vellykket registering.';
                vm.error = '';
            }).catch(function(error) {
                console.log(error);
            });
        }
    }
}

};

@laerkeli laerkeli changed the title from Mongo Error: 11000 duplicate key error collection? to ValidatingError: MongooseError Jan 9, 2017
@laerkeli laerkeli changed the title from ValidatingError: MongooseError to ValidatingError: MongooseError AND MongoError E11000 Jan 9, 2017
@varunjayaraman
Collaborator
  1. for the duplicate key error, are you registering with the same username or are you clearing your database before reregistering? The username field is unique, so you can't enter the same username twice. The error is pretty clear I think regarding that.

  2. for the validation error regarding the username, can you log the object before it gets created on the server? Or can I at least see a code snippet of how you're creating this on the backend?

Also, it helps if you format your code. Some of your code snippets were formatted, but the rest were incredibly difficult to read. Try wrapping the code in three backticks ```

@laerkeli

Sorry, for the mistakes, I've just began coding JavaScript the past few month, and I don't understand all my code, just some parts of it.

  1. Yes, right now my database it empty, well, I cleared it and made a new user, and I couldn't and got the same validation error.

Not sure, but I have my users controller:
skaermbillede 2017-01-11 kl 08 16 37

@vkarpov15
Collaborator

@laerkeli how did you clear your database?

@laerkeli

I go into mongo in the terminal and then find my db, and then the collection - then I remove all of the users in the database.

@vkarpov15
Collaborator

Please be a little more detailed in your description, code samples and shell output is helpful. Also, are you sure you're connecting to the same database and using the same collection? Which collection are your users stored in?

@laerkeli
laerkeli commented Jan 24, 2017 edited

I'm pretty sure I'm connected to the right database. I use the collection called "boders" (which have my stalls data) - And I can see the stalls data in my web application and in my database.
skaermbillede 2017-01-24 kl 09 58 44

I created a user once in my application - and it was stored in the collection called "users", and I could see it in the "users" collection - but I deleted them all, because I couldn't make a new user (and someone told me to try and clear my users).

skaermbillede 2017-01-24 kl 09 55 07

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment