Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. app.post( "/api/users", function( req, res ) {
  2.     var username  = req.body.username;
  3.     var password  = req.body.password;
  4.     var firstName = req.body.firstName;
  5.     var lastName  = req.body.lastName;
  6.     var email     = req.body.email;
  7.     //Create a new instance of our model
  8.     var newUser = new User( {
  9.         username : username,
  10.         password : password,
  11.         firstName: firstName,
  12.         lastName : lastName,
  13.         email    : email
  14.     } );
  15.     //Save our new user
  16.     //I don't know what the best way would be to send a different response within the newUser.save function based on whether or not there's an error.
  17.     //For example, if there's an error, I could do res.send( "Error" );
  18.     //If not, I could send res.send( "Success" );
  19.     //But the res var doesn't exist in the scope of the callback.
  20.     newUser.save( function( err ) {
  21.         //Something went wrong
  22.         if( err ) {
  23.             console.log( "Houston, we have a problem:", err );
  24.             return false;
  25.         }
  26.         console.log( "Created a new user with the name: " + username );
  27.     } );
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement