我正在尝试根据学生ID更新此处的特定学生详细信息。我不知道如何使用findById更新整个学生详细信息。 我能够从前端获取更新的值并将其发送到服务器,但是从服务器我可以将更新的值发送到mongoDB,但我不知道如何更新。 请帮帮我。
这是我的服务器代码:
server.post('/update',urlencodedParser,function(req,res){
var response={
Id:req.body.Id,
Fname : req.body.Fname,
age:req.body.age,
Lname : req.body.Lname,
dob : req.body.dob,
pob : req.body.pob,
month : req.body.month,
nation : req.body.nation,
mothertongue : req.body.mothertongue,
bloodgroup : req.body.bloodgroup,
fatherfname : req.body.fatherfname,
fatherlname : req.body.fatherlname,
occupation : req.body.occupation,
placeofwork : req.body.placeofwork,
officaladd : req.body.officaladd,
emailid : req.body.emailid,
phoneno : req.body.phoneno,
mobileno : req.body.mobileno,
motherfname : req.body.motherfname,
motherlname : req.body.motherlname,
motheroccupation : req.body.motheroccupation,
motherplaceofwork : req.body.motherplaceofwork,
motherofficaladd : req.body.motherofficaladd,
motheremailid : req.body.motheremailid,
motherphoneno : req.body.motherphoneno,
mothermobileno : req.body.mothermobileno,
adress : req.body.adress,
emergencyadress : req.body.emergencyadress,
emergencyphone1 : req.body.emergencyphone1,
emergencyphone2 : req.body.emergencyphone2,
relationship1 : req.body.relationship1,
relationship2 : req.body.relationship2
}
databaseInterface.updateStudent(response, function(err, valss){
if(err) res.send('ERROR!');
console.log(valss);
res.send(valss);
})
})
这是我的猫鼬代码:
function updateStudent(response,callback) {
console.log(response)
User.findById(response.Id, function(err, studentcollection2) {
if (err) return callback(err);
studentcollection2 = response;
return callback(null, studentcollection2);
});
}
答案 0 :(得分:0)
Mongoose提供了一个特定的功能来查找和更新。你缺少的主要薄是{$ set:response}。 Mongoose将使用响应中的相应值替换存储ID中的每个值。只需确保您的Mongoose Schema for User将允许所有这些。代码的第二部分应该是这样的:
function updateStudent(response,callback) {
console.log(response)
User.findByIdAndUpdate(response.Id, {$set:response},function(err, studentcollection2) {
if (err) return callback(err);
return callback(null, 'success');
});
}
答案 1 :(得分:0)
这是我想出的答案。
function updateStudent(response,callback) {
console.log(response.Id)
User.update({'Id':response.Id}, {$set:{
'Firstname':response.Fname,
'Age' : response.age,
'Lastname' : response.Lname,
'DateOfBirth' : response.dob,
'PlaceOfBirth' : response.pob,
'Months' : response.month,
'Nationality' : response.nation,
'MotherTongue' : response.mothertongue,
'BloodGroup' : response.bloodgroup,
'Father.Firstname' : response.fatherfname,
'Father.Lastname' : response.fatherlname,
'Father.Occupation' : response.occupation,
'Father.PlaceOfWork' : response.placeofwork,
'Father.OfficialAddress' : response.officaladd,
'Father.EmailId' : response.emailid,
'Father.PhoneNo' : response.phoneno,
'Father.MobileNo' : response.mobileno,
'Mother.Firstname' : response.motherfname,
'Mother.Lastname' : response.motherlname,
'Mother.Occupation' : response.motheroccupation,
'Mother.PlaceOfWork' : response.motherplaceofwork,
'Mother.OfficialAddress' : response.motherofficaladd,
'Mother.EmailId' : response.motheremailid,
'Mother.PhoneNo' : response.motherphoneno,
'Mother.MobileNo' : response.mothermobileno,
'ResidentialAddress' :response.adress,
'EmergencyContactNumber.Address' : response.emergencyadress,
'EmergencyContactNumber.PhoneNo1' : response.emergencyphone1,
'EmergencyContactNumber.PhoneNo2' : response.emergencyphone2,
'EmergencyContactNumber.Relationship1' : response.relationship1,
'EmergencyContactNumber.Relationship2' : response.relationship2
}
}, function(err, studentcollection2) {
console.log(studentcollection2);
if (err) return callback(err);
return callback(null, 'success');
});
}