April 2020
Intermediate to advanced
716 pages
18h 55m
English
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 ...