Guest User

Untitled

a guest
Jan 16th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. const userSchema = new mongoose.Schema({
  2. username: { type: String },
  3. email: { type: String },
  4. isVerified: { type: Boolean, default: false },
  5. picVersion: { type: String, default: '1531305955' },
  6. picId: { type: String, default: 'default.png' },
  7. images:{
  8. [
  9. {
  10. imgId: { type: String, default: '' },
  11. imgVersion: { type: String, default: '' }
  12. }
  13. ],
  14. validate: [arrayLimit, 'You can upload only 4 images']
  15. }
  16. city: { type: String, default: '' },
  17. });
  18.  
  19.  
  20. function arrayLimit(val) {
  21. return val.length <= 4;
  22. }
  23.  
  24. UploadImage(req, res) {
  25. cloudinary.uploader.upload(req.body.image, async result => {
  26. await User.update(
  27. {
  28. _id: req.user._id
  29. },
  30. {
  31. $push: {
  32. images: {
  33. imgId: result.public_id,
  34. imgVersion: result.version
  35. }
  36. }
  37. }
  38. )
  39. .then(() =>
  40. res
  41. .status(HttpStatus.OK)
  42. .json({ message: 'Image uploaded successfully' })
  43. )
  44. .catch(err =>
  45. res
  46. .status(HttpStatus.INTERNAL_SERVER_ERROR)
  47. .json({ message: 'Error uploading image' })
  48. ...
  49.  
  50. const userSchema = new mongoose.Schema({
  51. username: { type: String },
  52. email: { type: String },
  53. isVerified: { type: Boolean, default: false },
  54. picVersion: { type: String, default: '1531305955' },
  55. picId: { type: String, default: 'default.png' },
  56. images: {
  57. type:[{
  58. imgId: { type: String, default: '' },
  59. imgVersion: { type: String, default: '' }
  60. }],
  61. validate: [arrayLimit, 'You can upload only 4 images']
  62. },
  63. city: { type: String, default: '' },
  64. });
  65.  
  66.  
  67. function arrayLimit(val) {
  68. return val.length <= 4;
  69. }
  70.  
  71. let user = await User.findById(req.user._id);
  72. user.images.push({
  73. imgId: result.public_id,
  74. imgVersion: result.version
  75. });
  76. await user.save();
Add Comment
Please, Sign In to add comment