0

I have a post request for updating a product in stripe, but my issue is if I don't send a file in the request I get a 500 error and says UNEXPECTED END OF FORM AT MULTIPART. If I do upload a file, it works just fine. But i want it to be optional

I have tried setting an if statement to check if there is a req.file to retrieve the data I am looking for, but it still errors out. The fieldname and name of my input is correct, I have more than double checked.

stripeRouter.post('/update-product/:id',
  // validate,
  upload.single('imgURL'),
  async (req: express.Request, res: express.Response) => {
    const imgFile = req.file as Express.MulterS3.File
    log.info(`&&& New image file: ${JSON.stringify(imgFile)}`)
    
    const reqProduct = req.body;
    log.info(`&&& Request Product: ${JSON.stringify(reqProduct)}`)
    
    const productId = req.params.id;
    log.info(`&&& Product ID: ${productId}`)

    try {
      if (!reqProduct.name || !reqProduct.industry|| !reqProduct.price) return res.status(400).json({ error: 'product name, price and product industry is required.' });
      
      const stripe = await getStripe();
      
      if (imgFile) {
          // const imgFile = req.file as Express.MulterS3.File;
          reqProduct.imgURL = imgFile.location 
      }
        const currentProduct = await stripe.products.retrieve(productId);
        reqProduct.imgURL = currentProduct.metadata.img;
JD258
  • 1
  • 1

0 Answers0