Advertisement
Fahim_7861

Untitled

May 31st, 2021
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. router.post('/add', async (req, res) => {
  2.  
  3.  
  4.  
  5. try {
  6.  
  7. const allPic = [];
  8.  
  9.  
  10.  
  11. const Allfile = req.files.file;
  12.  
  13. Allfile.forEach(async file=>
  14. {
  15.  
  16. allPic.push(file.name);
  17.  
  18. console.log(file.name);
  19. })
  20.  
  21.  
  22.  
  23. Allfile.forEach(async file=>
  24. {
  25. await file.mv(`${__dirname.replace('routeHandler','')}images/${file.name}`);
  26.  
  27.  
  28. })
  29.  
  30.  
  31.  
  32.  
  33.  
  34. //await file.mv(`${__dirname.replace('routeHandler','')}images/${file.name}`);
  35.  
  36. req.body.pic= allPic;
  37.  
  38. console.log(allPic)
  39.  
  40. // return res.status(200).json({
  41.  
  42. // insertCount: true,
  43. // message: "Successfully Inserted",
  44. // });
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. console.log(req.body);
  52.  
  53. req.body.category = {
  54. id : req.body.categoryId,
  55. title: req.body.categoryTitle
  56. }
  57.  
  58. console.log(req.body);
  59.  
  60.  
  61. const newProduct = new Product(req.body);
  62.  
  63. const product = await newProduct.save()
  64.  
  65. await Category.updateOne({
  66. _id : req.body.category.id
  67. },
  68. {
  69. $push : {
  70. products : product.id
  71. }
  72. })
  73.  
  74. return res.status(200).json({
  75.  
  76. insertCount: true,
  77. message: "Successfully Inserted",
  78. }
  79. )
  80.  
  81.  
  82.  
  83. }
  84.  
  85. catch (err) {
  86.  
  87. res.status(500).json({
  88. insertCount: false,
  89. error: 'There was a server site error',
  90. })
  91.  
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement