Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Mongoose CREATE

//Add a user
/* We’ll expect JSON in this format
{
  ID: Integer,
  Username: String,
  Password: String,
  Email: String,
  Birthday: Date
}*/
app.post('/users', (req, res) => {
  Users.findOne({ Username: req.body.Username })
    .then((user) => {
      if (user) {
        return res.status(400).send(req.body.Username + 'already exists');
      } else {
        Users
          .create({
            Username: req.body.Username,
            Password: req.body.Password,
            Email: req.body.Email,
            Birthday: req.body.Birthday
          })
          .then((user) =>{res.status(201).json(user) })
        .catch((error) => {
          console.error(error);
          res.status(500).send('Error: ' + error);
        })
      }
    })
    .catch((error) => {
      console.error(error);
      res.status(500).send('Error: ' + error);
    });
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: chess game in javascript github 
Javascript :: how to use the foreach method in javascript 
Javascript :: json to string 
Javascript :: react particles 
Javascript :: how to import scss file in angular 
Javascript :: Async return values 
Javascript :: detect form input changes javascript 
Javascript :: make 2div in row react js 
Javascript :: javascript function return boolean 
Javascript :: mail 
Javascript :: string to uppercase 
Javascript :: useeffect hook 
Javascript :: how to swap two images in javascript 
Javascript :: javascript select option based on text 
Javascript :: nestjs swagger 
Javascript :: javascript check if string ends with space 
Javascript :: password reset passport-local mongoose 
Javascript :: tables javascript 
Javascript :: mysql json 
Javascript :: Adding User And Hashed Password In ExpressJS 
Javascript :: alertify js vue 
Javascript :: js map array to object 
Javascript :: delete duplicate array javascript 
Javascript :: node save wav base64 
Javascript :: firebase user sign out 
Javascript :: new line javascript string 
Javascript :: flutter response to json 
Javascript :: change class js 
Javascript :: charcodeat javascript 
Javascript :: javascript set style attribute 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =