I am having a problem with the user model that I'm using with Mongoose and MongoDB to create each profile in my database.(我在Mongoose和MongoDB上使用的用户模型在数据库中创建每个配置文件时遇到问题。)
It works fine to post one user, but throws the following error if I logout and try again:(发布一个用户可以正常工作,但是如果我注销并重试,则会引发以下错误:)
{
"name": "MongoError",
"message": "E11000 duplicate key error collection: CourtAPIDev.users index: trackers.case_id_1 dup key: { : null }",
"driver": true,
"index": 0,
"code": 11000,
"errmsg": "E11000 duplicate key error collection: CourtAPIDev.users index: trackers.case_id_1 dup key: { : null }"
}
According to mongoose documentation: If there is more than one document (a second user) without a value for the indexed field or is missing the indexed field, the index build will fail with a duplicate key error.(根据猫鼬的文档: 如果有多个文档(第二个用户)没有索引字段的值,或者缺少索引字段,则索引构建将失败,并出现重复的键错误。)
I don't know how to set this _id property for the trackers property –– I thought it generated automatically!(我不知道如何为trackers属性设置此_id属性–我以为它是自动生成的!)
Here's the trackers part of my Schema.(这是我的架构的跟踪器部分。)
And the relevant case_id property, which seems to be throwing the "null" error.(以及相关的case_id属性,该属性似乎抛出了“ null”错误。)
The whole repository can be found on my Github here, but the likely problem spots are the ones I highlighted, I think.(整个存储库都可以在我的Github上找到,但是我认为可能是我强调的问题点。)
Here's the github link: https://github.com/KingOfCramers/node_login_with_trackers(这是github链接: https : //github.com/KingOfCramers/node_login_with_trackers)
user model:(用户模型:)
const UserSchema = new mongoose.Schema({
email: {
type: String,
required: true,
trim: true,
minLength: 1,
unique: true,
validate: {
validator: (value) => {
return validator.isEmail(value);
},
message: '{VALUE} is not a valid email'
}
},
password: {
type: String,
required: true,
minlength: 6
},
tokens: [{
access: {
type: String,
required: true
},
token: {
type: String,
required: true
}
}],
trackers: {
tweets: [TwitterSchema],
legislation: [LegislationSchema],
court_cases: [CourtCaseSchema]
},
frequency: [EmailSchema]
});
Express route:(快速路线:)
app.post("/users", (req,res) => {
var body = _.pick(req.body, ['email', 'password']);
body.frequency = {
alert_time: new Date(),
email: req.body.email
}
var user = new User(body);
user.save().then(() => {
return user.generateAuthToken();
}).then((token) => {
res.header("x-auth", token);
res.send(user);
}).catch((e) => {
res.status(400).send(e);
});
});
Test (mocha):(测试(摩卡):)
it("Should post a new user", (done) => {
var email = "[email protected]"
var password = "9webipasd"
supertest(app)
.post("/users") // Post request to the /todos URL
.send({
email,
password
})
.expect(200)
.expect((res) => {
expect(res.headers).toIncludeKey('x-auth')
expect(res.body._id).toExist();
expect(res.body.email).toBe(email);
})
.end((err) => {
if(err){
return done(err);
}
User.findOne({email}).then((user) => {
expect(user).toExist();
expect(user.password).toNotBe(password);
done();
}).catch((e) => done(e));
});
});
ask by Harry Cramer translate from so