2017-02-15 37 views
0

我想根據學生id更新這裏的特定學生詳細信息。我不知道如何使用findById更新整個學生的詳細信息。 我能夠從前端獲取更新值並將其發送到服務器,但從服務器我能夠發送更新值到mongoDB,但我不知道如何更新。 請幫我一把。如何使用貓鼬更新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

這是我想出的答案。

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'); 
      }); 
     } 
0

貓鼬一種特定的功能來查找和更新。您缺少的主要功能是{$ set:response}。貓鼬將用響應中的相應值替換存儲的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'); 
     }); 
    } 
+0

它返回undefined。我正在獲得ERORR!作爲迴應 –

+0

req.body.Id等同於存儲在Mongodb中的id(用於索引的id,24個字符長)? –

+0

沒有。我正在使用自定義ID –