Skip to Content
Full-Stack React Projects - Second Edition
book

Full-Stack React Projects - Second Edition

by Shama Hoque
April 2020
Intermediate to advanced
716 pages
18h 55m
English
Packt Publishing
Content preview from Full-Stack React Projects - Second Edition

Create controller method

The create controller method, defined in the order controllers, is the last method that's invoked when the create order API receives a request. This method takes the order details, creates a new order, and saves it to the Order collection in MongoDB. The create controller method is implemented as follows.

mern-marketplace/server/controllers/order.controller.js:

const create = async (req, res) => {  try {    req.body.order.user = req.profile    let order = new Order(req.body.order)    let result = await order.save()    res.status(200).json(result)  } catch (err){    return res.status(400).json({      error: errorHandler.getErrorMessage(err)    })  }}

With this implemented, orders can be created and stored in the backend by any signed-in user ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Full-Stack React Projects

Full-Stack React Projects

Shama Hoque
React Native - The Practical Guide

React Native - The Practical Guide

Academind by Maximilian Schwarzmüller GmbH

Publisher Resources

ISBN: 9781839215414Supplemental Content