我正在尝试为"家庭创建一个记录"在Express路线中使用Mongoose的对象。我在创建一个家庭的第二次尝试中遇到了这个错误:
{ BulkWriteError: E11000 duplicate key error index: treemaker.families.$members.parents.id_1 dup key: { : null }
我不确定这意味着什么。这是完整的路线。猫鼬似乎最终被绊倒了:
router.post('/', jwtAuth, jsonParser, (req, res) => {
// checking that given id is valid
let { family_name, password, username, id } = req.body;
User.findOne({ _id: id})
.count()
.then(count => {
if (count < 1) {
return Promise.reject({
code: 422,
reason: 'Validation Error',
message: 'Family must be created by a user',
location: 'id'
})
}
return Promise.resolve();
})
.catch(err => {
return res.sendStatus(422).json({code: err.code, message: err.message, reason: err.reason, location: err.location })
});
// checking that required fields are present
const requiredFields = ['family_name', 'username', 'password'];
const missingField = requiredFields.find(field => !(field in req.body));
if(missingField) {
return res.status(422).json({
code: 422,
reason: 'Validation Error',
message: 'Missing field',
location: missingField
});
}
// checking the format of string fields
const stringFields = ['family_name', 'password', 'username'];
const nonStringField = stringFields.find(
field => field in req.body && typeof req.body[field] !== 'string'
);
if (nonStringField) {
return res.status(422).json({
code: 422,
reason: 'Validation Error',
message: 'Incorrect field type: expected string',
location: nonStringField
});
}
// checking the trimming on fields
const trimmedFields = ['family_name', 'password', 'username'];
const nonTrimmedField = trimmedFields.find(
field => req.body[field].trim() !== req.body[field]
);
if (nonTrimmedField) {
return res.status(422).json({
code: 422,
reason: 'Validation Error',
message: 'Cannot start or end with whitespace',
location: nonTrimmedField
});
}
// checking the length of fields
const sizedFields = {
family_name: { min: 1 },
password: { min: 10, max: 72 },
username: { min: 10, max: 72 }
};
const tooSmallField = Object.keys(sizedFields).find(field =>
'min' in sizedFields[field] &&
req.body[field].trim().length < sizedFields[field].min
)
const tooLargeField = Object.keys(sizedFields).find(field =>
'max' in sizedFields[field] &&
req.body[field].trim().length < sizedFields[field].min
)
if ( tooSmallField || tooLargeField) {
return res.status(422).json({
code: 422,
reason: 'Validation Error',
message: tooSmallField
? `Must be at least ${sizedFields[tooSmallField].min} characters long`
: `Must be at most ${sizedFields[tooLargeField].max} characters long`,
location: tooSmallField || tooLargeField
})
}
// checking existance of family with same username
return Family.find({ username })
.count()
.then(count => {
if(count > 0) {
return Promise.reject({
code: 422,
reason: 'Validation Error',
message: 'Username already taken',
location: 'username'
});
}
// creating family
return Family.hashPassword(password);
})
.then(hash => {
// HERE IS THE ISSUE
return Family.create({
username,
family_name,
password: hash,
members: []
})
})
.then(family => {
return res.status(201).json(family.apiRepr());
})
.catch(err => {
console.error(err);
return res.status(err.code).json({code: err.code, message: err.message, reason: err.reason, location: err.location })
});
});
错误似乎来自Family.create()部分。但我无法弄清楚为什么......有什么想法?
我注意到,不知怎的,&#34; id&#34;最初在请求中发送的内容以某种方式显示在数据库中...我已附上以下记录。 $ oid值是用户的id。我不希望那是家庭的id。怎么会发生这种情况?我不是将这些信息作为Family.create()...
的一部分发送{
"_id": {
"$oid": "5a79eb5f9c36f5112e623196"
},
"members": [],
"family_name": "HallissyFamily",
"password": "$2a$10$a0XbtTonq9b0BmSi2OHIMObDQjDER/yyZxdvN58nUC93SBtrnehr2",
"username": "something else",
"__v": 0
}