Today in the docking interface encountered some problems, because in order to facilitate the verification interface to write the right is to the database inside of some of their own data, the beginning of the use of the joy, and later encountered an update error.
For example, our app is a medical app just started to register when the default is the patient, as follows
When a doctor wants to register to make money in this app, you need to register to submit your own information, as follows
And then, according to the truth, will be registered successfully but reported after applying the update to the document {_id:3, ...}, the (immutable) field \ ' _id\ ' was found to the been altered to _id:17 '}
I didn't know what the hell was going on. Later asked and I just came to the colleague (also rookie) said is the database inside the first is manually inserted data, and then update the time _id repeated conflict, then listened to delete a lot of data, finally really can, but I am a rookie I think this is not the essence of the problem.
Then I kept checking the information and then I knew the problem was that I wrote the code with the following problems:
//Doctor certification
router.post ('/doctorauth ', function (req,res) {
Console.log (' Request object: ', req.body);
User.update (
{
_id:req.body.doctor_id,
Nickname:req.body.nickname,
Hospital:req.body.hospital,
address:req.body.address,
License:req.body.license,
Photo:req.body.photo,
Identity:enums.identityPerson.doctor,
State:enums.authState.pass,
Department:req.body.department,
speciality:req.body.speciality
},function (err,doctorauth) {
if (err) throw err;
if (doctorauth.nmodified = = 0 && DOCTORAUTH.N = = 0) {
res.send (errors.e112);
return;
}
Console.log (Doctorauth);
res.send (ERRORS.E0);
})
inside I update to bring _id, to know that MongoDB _id is not updated. Finally find the problem, and then simply modify the code as follows:
//Doctor certification
router.post ('/doctorauth ', function (req,res) {
Console.log (' Request object: ', req.body);
User.update (
{
_id:req.body.doctor_id
},
{
Nickname:req.body.nickname,
Hospital:req.body.hospital,
address:req.body.address,
License:req.body.license,
Photo:req.body.photo,
Identity:enums.identityPerson.doctor,
State:enums.authState.pass,
Department:req.body.department,
speciality:req.body.speciality
},function (err,doctorauth) {
if (err) throw err;
if (doctorauth.nmodified = = 0 && DOCTORAUTH.N = = 0) {
res.send (errors.e112);
return;
}
Console.log (Doctorauth);
res.send (ERRORS.E0);
})
});
and then it's ready. After eating a big loss to find a lesson, will never make such a problem again. Maybe this is not a problem in front of you, but for me this rookie to solve the feeling very happy O (∩_∩) o~~
The story between MongoDB data update and _ID