Advertisement
Fahim_7861

Untitled

Jun 19th, 2021
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1.  
  2.  
  3. const exporess = require("express");
  4.  
  5. const router = exporess.Router();
  6.  
  7. const mongoose = require("mongoose");
  8.  
  9. const orderHistorySchemas = require('../Schemas/orderHistorySchema');
  10.  
  11. const OrderHistory = new mongoose.model('OrderHistory',orderHistorySchemas);
  12.  
  13. const userSchema = require("../Schemas/userSchema");
  14.  
  15. const User = new mongoose.model("User", userSchema);
  16.  
  17.  
  18. router.post("/", async (req, res) => {
  19.  
  20.  
  21.  
  22.  
  23. try {
  24.  
  25.  
  26.  
  27.  
  28. req.body.orderInfo = JSON.parse(req.body.orderInfo)
  29.  
  30.  
  31.  
  32. const newOrderHistory = new OrderHistory(req.body);
  33.  
  34. await newOrderHistory.save()
  35.  
  36. console.log('ok')
  37.  
  38. console.log(newOrderHistory._id);
  39.  
  40. await User.updateOne({
  41. _id : req.body.userInfo
  42. },{
  43. $push: {
  44.  
  45. orderHistory : newOrderHistory._id
  46.  
  47. }
  48. })
  49. res.status(200).json({
  50. message: "Order was inserted successfully!",
  51. })
  52. }
  53.  
  54. catch (err) {
  55. res.status(500).json({
  56. msg : err,
  57. error: "There was a server side error!",
  58. });
  59. }
  60. });
  61.  
  62. module.exports = router;
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement