Advertisement
Guest User

Untitled

a guest
Aug 5th, 2017
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.04 KB | None | 0 0
  1. test1 [
  2.  
  3. [0] { test: Array, comments: Array },
  4. [1] { test: Array, comments: Array }
  5.  
  6. ]
  7.  
  8. var UserSchema = new Schema({
  9.  
  10. test1: { type: Array, required: false },
  11. test2: { type: Array, required: false },
  12. test3: { type: Array, required: false }
  13.  
  14. });
  15.  
  16. <form name="edit.test1" ng-submit="ctrl.updateTest1(newComment1, newComment2, ctrl.artikel)">
  17.  
  18. <div class="form-group">
  19. <label>Kommentarer:</label>
  20. <input class="form-control" type="text" name="test1" placeholder="Comment on first value" ng-model="newComment1" autocomplete="off">
  21. <br>
  22. <input class="form-control" type="text" name="test1" placeholder="Comment on second value" ng-model="newComment2" autocomplete="off">
  23. </div>
  24.  
  25. <button type="submit" class="btn btn-primary">Submit</button>
  26.  
  27. </form>
  28.  
  29. app.updateTest1 = function(newComment1, newComment2, index) {
  30. app.errorMsg = false; // Clear any error message
  31. app.disabled = true; // Lock form while processing
  32. // Check if username submitted is valid
  33.  
  34. var userObject = {}; // Create the user object to pass to function
  35. userObject._id = app.currentUser; // Pass current user _id in order to edit
  36.  
  37. userObject.test1 = [$scope.newComment1, $scope.newComment2, index]; // Set the new username provided
  38.  
  39. // Runs function to update the user's username
  40. User.editUser(userObject).then(function(data) {
  41.  
  42. // Behöver jag lägga till något här??
  43.  
  44. });
  45. };
  46.  
  47. userFactory.editUser = function(id) {
  48. return $http.put('/api/edit', id);
  49. };
  50.  
  51. router.post('/users', function(req, res) {
  52. var user = new User(); // Create new User object
  53. user.username = req.body.username; // Save username from request to User object
  54. user.password = req.body.password; // Save password from request to User object
  55. user.email = req.body.email; // Save email from request to User object
  56. user.name = req.body.name; // Save name from request to User object
  57. user.temporarytoken = jwt.sign({ username: user.username, email: user.email }, secret, { expiresIn: '24h' }); // Create a token for activating account through e-mail
  58.  
  59. // Check if request is valid and not empty or null
  60. if (req.body.username === null || req.body.username === '' || req.body.password === null || req.body.password === '' || req.body.email === null || req.body.email === '' || req.body.name === null || req.body.name === '') {
  61. res.json({ success: false, message: 'Ensure username, email, and password were provided' });
  62. } else {
  63. // Save new user to database
  64. user.save(function(err) {
  65. if (err) {
  66. // Check if any validation errors exists (from user model)
  67. if (err.errors !== null) {
  68. if (err.errors.name) {
  69. res.json({ success: false, message: err.errors.name.message }); // Display error in validation (name)
  70. } else if (err.errors.email) {
  71. res.json({ success: false, message: err.errors.email.message }); // Display error in validation (email)
  72. } else if (err.errors.username) {
  73. res.json({ success: false, message: err.errors.username.message }); // Display error in validation (username)
  74. } else if (err.errors.password) {
  75. res.json({ success: false, message: err.errors.password.message }); // Display error in validation (password)
  76. } else {
  77. res.json({ success: false, message: err }); // Display any other errors with validation
  78. }
  79. } else if (err) {
  80. // Check if duplication error exists
  81. if (err.code == 11000) {
  82. if (err.errmsg[61] == "u") {
  83. res.json({ success: false, message: 'That username is already taken' }); // Display error if username already taken
  84. } else if (err.errmsg[61] == "e") {
  85. res.json({ success: false, message: 'That e-mail is already taken' }); // Display error if e-mail already taken
  86. }
  87. } else {
  88. res.json({ success: false, message: err }); // Display any other error
  89. }
  90. }
  91. } else {
  92. // Create e-mail object to send to user
  93. var email = {
  94. from: 'MEAN Stack Staff, cruiserweights@zoho.com',
  95. to: [user.email, 'gugui3z24@gmail.com'],
  96. subject: 'Your Activation Link',
  97. text: 'Hello ' + user.name + ', thank you for registering at localhost.com. Please click on the following link to complete your activation: http://www.herokutestapp3z24.com/activate/' + user.temporarytoken,
  98. html: 'Hello<strong> ' + user.name + '</strong>,<br><br>Thank you for registering at localhost.com. Please click on the link below to complete your activation:<br><br><a href="http://www.herokutestapp3z24.com/activate/' + user.temporarytoken + '">http://www.herokutestapp3z24.com/activate/</a>'
  99. };
  100. // Function to send e-mail to the user
  101. client.sendMail(email, function(err, info) {
  102. if (err) {
  103. console.log(err); // If error with sending e-mail, log to console/terminal
  104. } else {
  105. console.log(info); // Log success message to console if sent
  106. console.log(user.email); // Display e-mail that it was sent to
  107. }
  108. });
  109. res.json({ success: true, message: 'Account registered! Please check your e-mail for activation link.' }); // Send success message back to controller/request
  110. }
  111. });
  112. }
  113. });
  114.  
  115. router.put('/edit', function(req, res) {
  116. var editUser = req.body._id; // Assign _id from user to be editted to a variable
  117. if (req.body.name) var newName = req.body.name; // Check if a change to name was requested
  118. if (req.body.username) var newUsername = req.body.username; // Check if a change to username was requested
  119. if (req.body.email) var newEmail = req.body.email; // Check if a change to e-mail was requested
  120. if (req.body.permission) var newPermission = req.body.permission; // Check if a change to permission was requested
  121.  
  122. if (req.body.test1) {
  123. var newTest1 = req.body.test1;
  124. }
  125. if (req.body.test2) {
  126. var newTest2 = req.body.test2;
  127. }
  128. if (req.body.test3) {
  129. var newTest3 = req.body.test3;
  130. }
  131. if (req.body.test4) {
  132. var newTest4 = req.body.test4;
  133. }
  134. if (req.body.test5) {
  135. var newTest5 = req.body.test5;
  136. }
  137.  
  138.  
  139. // Look for logged in user in database to check if have appropriate access
  140. User.findOne({ username: req.decoded.username }, function(err, mainUser) {
  141. if (err) {
  142. // Create an e-mail object that contains the error. Set to automatically send it to myself for troubleshooting.
  143. var email = {
  144. from: 'MEAN Stack Staff, cruiserweights@zoho.com',
  145. to: 'gugui3z24@gmail.com',
  146. subject: 'Error Logged',
  147. text: 'The following error has been reported in the MEAN Stack Application: ' + err,
  148. html: 'The following error has been reported in the MEAN Stack Application:<br><br>' + err
  149. };
  150. // Function to send e-mail to myself
  151. client.sendMail(email, function(err, info) {
  152. if (err) {
  153. console.log(err); // If error with sending e-mail, log to console/terminal
  154. } else {
  155. console.log(info); // Log success message to console if sent
  156. console.log(user.email); // Display e-mail that it was sent to
  157. }
  158. });
  159. res.json({ success: false, message: 'Something went wrong. This error has been logged and will be addressed by our staff. We apologize for this inconvenience!' });
  160. } else {
  161. // Check if logged in user is found in database
  162. if (!mainUser) {
  163. res.json({ success: false, message: "no user found" }); // Return error
  164. } else {
  165. // Check if a change to name was requested
  166.  
  167. -----> HERE if (newTest1) {
  168. // Check if person making changes has appropriate access
  169. if (mainUser.permission === 'admin') {
  170. // Look for user in database
  171. User.findOne({ _id: editUser }, function(err, user) {
  172. if (err) {
  173. res.json({ success: false, message: 'Something went wrong. This error has been logged and will be addressed by our staff. We apologize for this inconvenience!' });
  174. } else {
  175. // Check if user is in database
  176. if (!user) {
  177. res.json({ success: false, message: 'No user found' }); // Return error
  178. } else {
  179.  
  180. if (Array.isArray(newTest1)) {
  181. ------> this does not work user.test1[0].comments.push(newTest1);
  182. //user.test1.splice(index, 0, newTest1)
  183. } else {
  184. ---> this works var testet1 = { test: newTest1.split(" "), comments: Array };
  185. user.test1.push(testet1); // Assign new name to user in database
  186. }
  187. // Save changes
  188. user.save(function(err) {
  189. if (err) {
  190. console.log(err); // Log any errors to the console
  191. } else {
  192. res.json({ success: true, message: 'Name has been updated!' }); // Return success message
  193. }
  194. });
  195. }
  196. }
  197. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement