Answer the question
In order to leave comments, you need to log in
Chat registration. Magic?
I write chat on node.js. Authorization I do this:
Enter the username and password. If there are, then let's go. If not, then register and let.
We will skip complex checks for now. And this is what we end up with. Registration works only on the first attempt. That is, we open the page, enter data from the bulldozer into the form and click Enter.
Everyone signed up, great. We refresh the page (at the same time, localstorage is cleared. This is instead of the Logout button) and try to register or log in with our login - and then the node crashes with an error.
Here is the user creation function:
function genSessionKey(){
var s ='';
var abd ='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
var aL = abd.length;
while(s.length < 30){
s += abd[Math.random() * aL|0];
}
var hash = SHA512(s).toString();
return hash;
}
// создаем пользователя в БД
function createUser(usr, callback) {
/**
* КАКАЯ-ТО ХУЙНЯ С МЕТОДОМ SAVE
*/
var usver = new User(usr);
var ssid = genSessionKey(); // <- генерируется нормально
usver.save({
login: usr.login,
password: SHA512(usr.password).toString(),
s_key: ssid // <- в БД эта строка не попадает. Даже ключ s_key не создается
}, function (err, ussr) {
if (err) {
throw err;
}
// не запрашиваем авторизацию, пускаем сразу
callback(true, ussr);
});
}
var UserSchema = new Schema({
login: { type: String, required: true, index: false, unique: true, validate: /[\da-zA-Z]/ },
password: { type: String, required: true },
color: { type: String, default: config.get('default_color') },
role: { type: Number, min: 0, max: 2, default: 0 },
avatar: { type: String, default: config.get('default_avatar') },
timestamp: { type: Date, default: TimeStamp.ISOget },
lastlogin: { type: Date, default: TimeStamp.ISOget },
online: { type: Boolean, default: true },
s_key: { type: String },
isBanned: { type: Number, min: 0, max: 1, default: 0 },
gender: { type: Number, min: 0, max: 2, default: 0 }
});
D:\NODEjs\projects\AppChat\node_modules\mongodb\lib\utils.js:98
process.nextTick(function() { throw err; });
^
MongoError: E11000 duplicate key error collection: AppChat.users index: ssid_1 dup key: { : null }
at Function.MongoError.create (D:\NODEjs\projects\AppChat\node_modules\mongodb\node_modules\mongodb-core\lib\error.js:31:11)
at toError (D:\NODEjs\projects\AppChat\node_modules\mongodb\lib\utils.js:114:22)
at D:\NODEjs\projects\AppChat\node_modules\mongodb\lib\collection.js:597:23
at handleCallback (D:\NODEjs\projects\AppChat\node_modules\mongodb\lib\utils.js:96:12)
at D:\NODEjs\projects\AppChat\node_modules\mongodb\lib\bulk\unordered.js:470:9
at handleCallback (D:\NODEjs\projects\AppChat\node_modules\mongodb\lib\utils.js:96:12)
at resultHandler (D:\NODEjs\projects\AppChat\node_modules\mongodb\lib\bulk\unordered.js:417:5)
at D:\NODEjs\projects\AppChat\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:780:13
at Callbacks.emit (D:\NODEjs\projects\AppChat\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:95:3)
at null.messageHandler (D:\NODEjs\projects\AppChat\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:249:23)
at Socket.<anonymous> (D:\NODEjs\projects\AppChat\node_modules\mongodb\node_modules\mongodb-core\lib\connection\connection.js:265:22)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:146:16)
at Socket.Readable.push (_stream_readable.js:110:10)
at TCP.onread (net.js:523:20)
Answer the question
In order to leave comments, you need to log in
Monga does not allow you to create a duplicate of the document,
judging by the error in the collection, uniqueness is set on the "ssid" field, but you do not pass it and it is written as empty, and because the field is unique, then 2 empty values \u200b\u200bin this field are not allowed.
Either remove the unique index on this field, or write a unique value to this field.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question