Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Strange behavior of display different error message for signup if username/email already exists on Mongo 3.0/3.2 #1245

Closed
QiyuLi opened this issue Mar 2, 2016 · 2 comments
Assignees
Milestone

Comments

@QiyuLi
Copy link
Contributor

QiyuLi commented Mar 2, 2016

@trendzetter @ilanbiala

I'm reading Mongoose doc. I did not find any info about configuring err msg for unique.
I'm running Ubuntu 14.04 with MongoDB 3.2.3. Nodejs is v4.3.2

I attached a screenshot. This is not good to display such a detailed error.
image
image

I tested with clean enviroment in vagrant. The only difference is: MongoDB 3.0.9 and 3.2.3.
I wonder where does the sentence "Email already exists" or '11000 duplicate key error collection: mean-test.users index: username already exists' to be 'Username already exists' come from?

I enabled the debug mode of mongoose. I tested with both Mongo 3.0 and 3.2.
The query generated by mongoose is the same (execept salt).
Then I execute the insert query manually in Mongo 3.0 and 3.2.
The output is also the same.

Mongo 3.0
Mongoose: users.insert({ salt: 'RphFQ5BmRcgAxoa9vAaCTQ==', displayName: 'Test User', provider: 'local', username: 'testuser', _id: ObjectId("56d767da3ccc59b408859f90"), created: new Date("Wed, 02 Mar 2016 22:23:22 GMT"), roles: [ 'user' ], profileImageURL: 'modules/users/client/img/profile/default.png', password: '9WsKha9q20AQdR6jYfBIv9LSYg5Qsfa+MTLFka0KmazFcGKh9VKVxFm1ft3S2AjqIVJorN4ihEz1nOoH6U22UA==', email: 'test@abc.com', lastName: 'User', firstName: 'Test', __v: 0 })
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error index: mean-dev.users.$username_1 dup key: { : "testuser" }"
}
})

Mongo 3.2
Mongoose: users.insert({ salt: 'd4SiQVR3xDk0rqHQ3dajNw==', displayName: 'Test User', provider: 'local', username: 'testuser', _id: ObjectId("56d7684543b5be880b8aedd7"), created: new Date("Wed, 02 Mar 2016 22:25:09 GMT"), roles: [ 'user' ], profileImageURL: 'modules/users/client/img/profile/default.png', password: 'hsKel5Gew/IVqoo8oynaOFayV35sKP07xbcApvVosAhILPg99l0ccv74+mu7k14+ph2LJpxQ5ntPsDBTMtYhag==', email: 'test@abc.com', lastName: 'User', firstName: 'Test', __v: 0 })
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error index: mean-dev.users.$username_1 dup key: { : "testuser" }"
}
})

I think mongodb does not change error reply. I wonder whether mongoose or other plugin changes the err msg behavior.

@QiyuLi
Copy link
Contributor Author

QiyuLi commented Mar 2, 2016

@trendzetter @ilanbiala

I figured out the problem.
If you upgrade mongodb from 3.0 to 3.2, there is no issue because you still use MMAPv1 engine.
But if you install new mongodb 3.2, there is a problem because you use new WiredTiger engine.

The output of these 2 engine is slightly different.
MMAPv1
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error index: mean-dev.users.$email_1 dup key: { : "test@user.com" }"
}
})

WiredTiger
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: mean-dev.users index: email_1 dup key: { : "test@user.com" }"
}
})

There is a minor diff between .$ and index:

The method in the file modules/core/server/controllers/errors.server.controller.js
is used to parse the error message and output it to front webpage:

/**

  • Get unique error field name
    */
    var getUniqueErrorMessage = function (err) {
    var output;

    try {
    var fieldName = err.errmsg.substring(err.errmsg.lastIndexOf('.$') + 2, err.errmsg.lastIndexOf('_1'));
    output = fieldName.charAt(0).toUpperCase() + fieldName.slice(1) + ' already exists';

    } catch (ex) {
    output = 'Unique field already exists';
    }

    return output;
    };

It only deals with .$
If we also parse index:, it will be done without modifying any test script.

@mleanos
Copy link
Member

mleanos commented Mar 3, 2016

@QiyuLi I don't quite follow your suggestion, but if you'd like to submit a PR we'd gladly review it. Thanks for getting to the bottom of this.

@mleanos mleanos self-assigned this Mar 3, 2016
@mleanos mleanos added this to the 0.5.0 milestone Mar 3, 2016
QiyuLi added a commit to QiyuLi/mean that referenced this issue Mar 4, 2016
The new WiredTiger engine is introduced in MongoDB 3.2.
It changes the output errmsg format for violation of unique index.
This commit adds support for the new format.

Fixes meanjs#1245
HarryGogonis pushed a commit to HarryGogonis/mean that referenced this issue Mar 18, 2016
The new WiredTiger engine is introduced in MongoDB 3.2.
It changes the output errmsg format for violation of unique index.
This commit adds support for the new format.

Fixes meanjs#1245
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants