Я работаю над своим первым приложением Express, и по какой-то причине Express / Mongo не может понять мой POST-запрос Postman при тестировании моей функции регистрации. Я получаю сообщение BulkWriteError, в котором утверждается, что имя пользователя не передано, а имя пользователя "null" уже занято. Я не знаю, что еще сказать о ситуации, но мне нужно добавить больше текста, чтобы можно было задать вопрос.
Users.js
// Register
router.post('/register', function (req, res, next) {
let username = req.body.username;
let email = req.body.email;
let password1 = req.body.password1;
let password2 = req.body.password2;
// Check
req.checkBody('username', "Username Is Required").notEmpty();
req.checkBody('email', "Invalid Email Adress").isEmail();
req.checkBody('password1', 'Password is required').notEmpty();
req.checkBody('password2', 'Passwords do not match').equals(req.body.password1);
let errors = req.validationErrors();
if(errors) {
console.log(errors);
} else {
MongoClient.connect(url, function(err, db) {
if (err) throw err;
let dbo = db.db('freeva');
dbo.collection('users').find({username: username}, function (err, match) {
if (err) throw err;
if (match.username == username) {
console.log('Username Taken');
db.close();
}
});
dbo.collection('users').find({email: email}, function (err, match) {
if (err) throw err;
if (match.email == email) {
console.log('Email Taken');
db.close();
}
});
});
let newUser = new User ({
username : username,
email : email,
password : password1,
Following : [],
FollowingCount : 0,
Followers : [],
FollowerCount: 0,
TextPosts : [],
Videos : [],
Views: 0,
EXP: 0,
Featured: true
});
User.addUser(newUser, function (err) {
if(err) {
console.log(err);
} else {
console.log("Registration Successful");
}
});
res.redirect('/users/login');
}
});
user.js
module.exports.addUser = function(newUser, callback){
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
if(err) throw err;
newUser.password = hash;
newUser.save(callback);
});
});
};
РЕДАКТИРОВАТЬ: Консоль
// Console.log(req.body);
enter code here
{ email: 'test@gmail.com',
username: 'Test',
password1: 'testpass',
password2: 'testpass' }
// If (err) throw err; (Error is from newUser.save in addUser function)
{ BulkWriteError: E11000 duplicate key error collection: freeva.users index: Username_1 dup key: { : null }
at /Users/willhamilton/Desktop/freeva/node_modules/mongodb/lib/bulk/unordered.js:535:15
at handleCallback (/Users/willhamilton/Desktop/freeva/node_modules/mongodb/lib/utils.js:128:55)
at resultHandler (/Users/willhamilton/Desktop/freeva/node_modules/mongodb/lib/bulk/unordered.js:456:5)
at /Users/willhamilton/Desktop/freeva/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:544:18
at process._tickCallback (internal/process/next_tick.js:176:11)
name: 'BulkWriteError',
message: 'E11000 duplicate key error collection: freeva.users index: Username_1 dup key: { : null }',
driver: true,
code: 11000,
index: 0,
errmsg: 'E11000 duplicate key error collection: freeva.users index: Username_1 dup key: { : null }',
getOperation: [Function],
toJSON: [Function],
toString: [Function],
result:
BulkWriteResult {
ok: [Getter],
nInserted: [Getter],
nUpserted: [Getter],
nMatched: [Getter],
nModified: [Getter],
nRemoved: [Getter],
getInsertedIds: [Function],
getUpsertedIds: [Function],
getUpsertedIdAt: [Function],
getRawResponse: [Function],
hasWriteErrors: [Function],
getWriteErrorCount: [Function],
getWriteErrorAt: [Function],
getWriteErrors: [Function],
getLastOp: [Function],
getWriteConcernError: [Function],
toJSON: [Function],
toString: [Function],
isOk: [Function] } }