<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Karrel</title>
    <description>The latest articles on DEV Community by Karrel (@sevda).</description>
    <link>https://dev.to/sevda</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1154658%2F40e2f3f2-bbdc-422c-a945-ddf8f5a62cb7.jpeg</url>
      <title>DEV Community: Karrel</title>
      <link>https://dev.to/sevda</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sevda"/>
    <language>en</language>
    <item>
      <title>Multer in javascript, node js Error in Uploading a Photo</title>
      <dc:creator>Karrel</dc:creator>
      <pubDate>Wed, 06 Sep 2023 08:32:01 +0000</pubDate>
      <link>https://dev.to/sevda/multer-in-javascript-node-js-error-in-uploading-a-photo-c12</link>
      <guid>https://dev.to/sevda/multer-in-javascript-node-js-error-in-uploading-a-photo-c12</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pbqZeLoF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6y60waa36mpm0wg32e8h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pbqZeLoF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6y60waa36mpm0wg32e8h.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hello guys can you help me fix the error in my gallery page. I don’t know where is the error in my code. I already troubleshoot it but the are the same. I use vuejs a javascript framework the image above are the error when I click the post button. &lt;br&gt;
Below are the code. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The upload photo file&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;const express = require('express');&lt;br&gt;
const multer = require('multer');&lt;br&gt;
const path = require('path');&lt;/p&gt;

&lt;p&gt;const app = express();&lt;br&gt;
const port = process.env.SERVER_PORT || 3000;&lt;/p&gt;

&lt;p&gt;// Set up storage for uploaded files&lt;br&gt;
const storage = multer.diskStorage({&lt;br&gt;
  destination: function (req, file, cb) {&lt;br&gt;
    cb(null, '../assets/uploads');&lt;br&gt;
  },&lt;br&gt;
  filename: function (req, file, cb) {&lt;br&gt;
    const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);&lt;br&gt;
    const fileExtension = path.extname(file.originalname);&lt;br&gt;
    cb(null, file.fieldname + '-' + uniqueSuffix + fileExtension);&lt;br&gt;
  }&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;const upload = multer({ storage: storage });&lt;/p&gt;

&lt;p&gt;// Serve uploaded files statically&lt;br&gt;
app.use('/uploads', express.static('uploads'));&lt;/p&gt;

&lt;p&gt;// Handle file upload&lt;br&gt;
app.post('/upload-photo', upload.single('image'), (req, res) =&amp;gt; {&lt;br&gt;
  if (!req.file) {&lt;br&gt;
    return res.status(400).json({ message: 'No file uploaded' });&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;// You can perform additional logic here, like saving the file path to a database&lt;br&gt;
  // and returning a response.&lt;br&gt;
  const { DB_CURRENT_TIMESTAMP, queryDb } = require('@api/utils/mysql');&lt;/p&gt;

&lt;p&gt;module.exports = async function(req, res) {&lt;br&gt;
  try {&lt;br&gt;
   const { image = '' } = req.body;&lt;/p&gt;

&lt;p&gt;if (!image) {&lt;br&gt;
   return res.status(400).json({&lt;br&gt;
    message: 'Please select an Image.',&lt;br&gt;
   });&lt;br&gt;
   }&lt;/p&gt;

&lt;p&gt;const imagePath = /uploads/${image.filename}; // Update the path to match your file storage location&lt;/p&gt;

&lt;p&gt;// Save file path to the database&lt;br&gt;
   await queryDb(&lt;br&gt;
   INSERT INTO gallery (date_uploaded, image) VALUES (?, ?),&lt;br&gt;
   [DB_CURRENT_TIMESTAMP, imagePath]&lt;br&gt;
   );&lt;/p&gt;

&lt;p&gt;return res.status(200).json({&lt;br&gt;
   message: 'Image Uploaded!'&lt;br&gt;
   });&lt;br&gt;
  } catch (err) {&lt;br&gt;
   res.status(500).json({&lt;br&gt;
   message: Server error : ${err.message}&lt;br&gt;
   });&lt;br&gt;
  }&lt;br&gt;
 };&lt;/p&gt;

&lt;p&gt;res.status(200).json({ message: 'Image Uploaded' });&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;app.listen(port, () =&amp;gt; {&lt;br&gt;
  console.log(Server is running on port ${port});&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;_To connect in database code file _&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;const { DB_CURRENT_TIMESTAMP, queryDb } = require('@api/utils/mysql');&lt;/p&gt;

&lt;p&gt;module.exports = async function(req, res) {&lt;br&gt;
  try {&lt;br&gt;
      var {&lt;br&gt;
          // event_id = '',&lt;br&gt;
          // photo_name = '',&lt;br&gt;
          image = '',&lt;br&gt;
      } = req.body;&lt;br&gt;
      // var image = req.files &amp;amp;&amp;amp; req.files.image;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  if (!image) {
      return res.status(400).json({
        message: 'Please select an Image.',
      });
  }
  // if (!event_id) {
  //     return res.status(400).json({
  //       message: 'Please provide the event Id.',
  //     });
  // }
  // if (!photo_name) {
  //     return res.status(400).json({
  //       message: 'Please provide a name for the photo.',
  //     });
  // }

  await queryDb(`
      INSERT INTO gallery (date_uploaded, image ) VALUES (?,?)`,
      [ DB_CURRENT_TIMESTAMP, image ]
  );

  return res.status(200).json ({
      message: 'Photo successfully added!'
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;
  catch(err) {&lt;br&gt;
      // console.log(err.message);&lt;br&gt;
      res.status(500).json({&lt;br&gt;
          message: &lt;code&gt;Server error : ${err.message}&lt;/code&gt;&lt;br&gt;
      });&lt;br&gt;
  }&lt;br&gt;
}&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
