SHARE
TWEET

Untitled

a guest Aug 19th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if(err){
  2.             res.render('addproduct',{
  3.                 mesg: err
  4.             });
  5.         }
  6.             new product({
  7.                 _id: new mongoose.Types.ObjectId(),
  8.                  image:req.body.image,
  9.                  name: req.body.name,
  10.                  price:  req.body.price,
  11.                  description: req.body.description,
  12.                  category: req.body.category,
  13.              }).save(function(err)
  14.              {
  15.                  if(err){
  16.                      console.log(err);
  17.                      res.render('addProduct')
  18.                  }else{
  19.                  res.redirect('/products');
  20.              }
  21.              })
  22.         });
  23.     })
  24.     //all function for uploading images and checking file
  25. //storage engine
  26. const storage= multer.diskStorage({
  27.     destination: './Public/images/',
  28.     filename: function(req, file, cb){
  29.         cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
  30.     }  
  31. });
  32. //upload method
  33. const upload = multer({
  34.     storage: storage
  35. }).single('image');
  36.      
  37. this is code for getting a file from ./public/image folder
  38. <div class= "two row">  
  39.                 <% products.forEach(product => { %>
  40.                     <div class="column">
  41.  
  42.                             <img src="./Public/images/<%req.files[0].filename%>" class="cover-img">
  43.                             <p><%= product.name %></p>
  44.                             <h3>&dollar;<%= product.price %></h3>
  45.                          <!---  <p><%= product.description %></p>-->
  46.                     </div>
  47.                <% }); %>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top