Guest User

Untitled

a guest
Jul 16th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. [
  2. {
  3. "_id": "5b4cebf353dc12159b769d9c",
  4. "name": "Profile_A",
  5. "handle": "profile_a",
  6. "date": "2018-07-16T19:03:15.341Z",
  7. "__v": 0
  8. },
  9. {
  10. "_id": "5b4d3957a5adce2572865b98",
  11. "name": "Profile_B",
  12. "handle": "profile_b",
  13. "date": "2018-07-17T00:33:27.266Z",
  14. "__v": 0
  15. },
  16. {
  17. "_id": "5b4d507bb71d9727ce8a5504",
  18. "name": "Profile_C",
  19. "handle": "profile_c",
  20. "date": "2018-07-17T02:12:11.204Z",
  21. "__v": 0
  22. }
  23. ]
  24.  
  25. // @POST api/profile/handle/:handle
  26. // @desc Update Profile By Handle
  27. router.post('/handle/:handle', (req, res) => {
  28. const profileFields = {};
  29. const { errorsObj, isValid } = validateProfile(req.body);
  30. const trimHandle = n => n.toLowerCase().replace(/s/g, '_');
  31. // Check Validation
  32. if (!isValid) {
  33. return res.status(404).json(errorsObj);
  34. }
  35. ProfileModel.findOne({ handle: req.params.handle })
  36. .then(profile => {
  37. if (!profile) {
  38. errorsObj.noprofile = 'There is no profile';
  39. res.status(404).json(errorsObj);
  40. }
  41.  
  42. if (req.body.name) profileFields.name = req.body.name;
  43. if (req.body.name) clientProfileFields.handle = trimHandle(req.body.name)
  44.  
  45. // UPDATE
  46. ProfileModel.findOneAndUpdate(
  47. { name: profileFields.name },
  48. { $set: profileFields },
  49. { new: true }
  50. ).then(profile => res.json(profile));
  51. })
  52. .catch(err => res.status(404).json(err));
Add Comment
Please, Sign In to add comment