Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. app.put("/api/students/:studentId", function(req, res) {
  2.  
  3.     var updateDoc = req.body;
  4.     delete updateDoc._id;
  5.  
  6.     db.collection(STUDENTS_COLLECTION).findOne({
  7.         studentId: parseInt(req.params.studentId)
  8.     }, function(err, doc) {
  9.         if (err) {
  10.             handleError(res, err.message, "No student with id " + req.params.studentId);
  11.         } else {
  12.             if (doc) {
  13.                 updateDoc.studentId = doc.studentId
  14.                 db.collection(STUDENTS_COLLECTION).updateOne({
  15.                     studentId: doc.studentId
  16.                 }, updateDoc, function(err, finalDoc) {
  17.                     if (err) {
  18.                         handleError(res, err.message, "Failed to update student");
  19.                     } else {
  20.                         res.status(200).json(finalDoc);
  21.                     }
  22.                 });
  23.             } else {
  24.                 handleError(res, "Invalid user input", "No student with id " + req.params.studentId);
  25.             }
  26.         }
  27.     });
  28.  
  29. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement