Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

mongoose post new document

//Router file. After doing all your imports at beginning of file
const router = express.Router();
app.use('/pathforpostrequests', router);

router.post('/', controller.create, (req, res, next) => {
  res.status(200).json(res.locals.person);
});

// Controller file. After doing all your imports at beginning of file. Person is an example mongo schema you will need to setup in a schema file.
const controller = {
  create (req, res, next) {
    const {firstName, lastName, age} = req.body;
    const newPerson = new Person ({firstName, lastName, age});
    res.locals.person = newPerson;
    newPerson.save();
    next();
  },
}
 
PREVIOUS NEXT
Tagged: #mongoose #post #document
ADD COMMENT
Topic
Name
1+5 =