2017-02-15 7 views
0

ここで特定の学生の詳細を学生IDに基づいて更新しようとしています。findByIdを使用して学生の詳細全体を更新する方法がわかりません。 フロントエンドから更新された値を取得してサーバーに送信できますが、サーバーから更新された値をmongoDBに送信できますが、更新方法はわかりません。 私を助けてください。mongooseを使用してmongodoseでオブジェクト全体を更新する方法

これは私のサーバーのコードです:

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}です。 Mongooseは、格納されたIDのすべての値を応答のそれぞれの値に置き換えます。あなたのMongooseスキーマがこれらすべてを許可することを確認してください。コードの2番目の部分は次のようになります。

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

戻り値は未定義です。私はERORRを手に入れています!レスポンスとして –

+0

req.body.IdはMongodbに格納されているIDと同じです(インデックスに使用されるID、24文字の長さ)? –

+0

番号。私はカスタムIDを使用しています –

関連する問題