Guest User

Untitled

a guest
Jul 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. <form action="/createaccount" id="myform" method="POST">
  2.  
  3. <label>Enter your first name</label>
  4. <input type="text" placeholder="First name" name="firstname">
  5.  
  6. </br><label>Enter your last name</label>
  7. <input type="text" placeholder="Last name" name="lastname">
  8.  
  9. </br><label for="uname">Enter a username</label>
  10. <input type="text" placeholder="Username" name="username">
  11.  
  12. </br><label for="psw">Enter a password</label>
  13. <input type="password" placeholder="Password" name="password">
  14.  
  15. </br><button type="submit">Create Account</button>
  16. <div id="error"></div>
  17.  
  18.  
  19. </form>
  20.  
  21. app.post('/createaccount', (req, res) => {
  22. new_username = req.body.username;
  23. new_password = req.body.password;
  24. new_firstname = req.body.firstname;
  25. new_lastname = req.body.lastname;
  26.  
  27. MongoClient.connect(url, (err, client) => {
  28. if (err) return console.log(err)
  29. db = client.db('entelligence')
  30.  
  31. var myobj = {
  32. firstName: new_firstname,
  33. lastName: new_lastname,
  34. username: new_username,
  35. password: new_password
  36. };
  37.  
  38. var match = false;
  39. var empty = false;
  40. var error_string = "";
  41. for (var i = 0; i < array.length; i++) {
  42. console.log(array[i].username);
  43. if (array[i].username == new_username) {
  44. match = true;
  45. }
  46. }
  47.  
  48. if (match) {
  49. error_string = "Username is already taken. "
  50. };
  51. if (new_firstname == '' || new_lastname == '' || new_username == '' || new_password == '') {
  52. error_string += "Field(s) cannot be empty";
  53. }
  54. console.log(error_string);
  55.  
  56. if (error_string == "") {
  57. db.collection("users").insertOne(myobj, function(err, res) {
  58. if (err) throw err;
  59. console.log("1 document inserted");
  60. });
  61. res.redirect('/');
  62. }
  63.  
  64. res.send(error_string);
  65.  
  66. })
  67. })
Add Comment
Please, Sign In to add comment