title: import CSV file into mysql database using nodejs
body: <p>I'm trying to upload a CSV file to MySQL database using nodejs... it's working successfully and rows added, but it's gave me an error.
  this is my code :</p>
  <pre><code>//use express static folder
  app.use(express.static(&quot;./public&quot;))
   
  // body-parser middleware use
  app.use(bodyParser.json())
  app.use(bodyParser.urlencoded({
      extended: true
  }))
   
   
  db.connect(function (err) {
      if (err) {
          return console.error('error: ' + err.message);
      }
      console.log('Connected to the MySQL server.');
  })
   
  //! Use of Multer
  var storage = multer.diskStorage({
      destination: (req, file, callBack) =&gt; {
          callBack(null, './uploads/')    
      },
      filename: (req, file, callBack) =&gt; {
          callBack(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
      }
  })
   
  var upload = multer({
      storage: storage
  });
   
  //! Routes start
   
  //route for Home page
  app.get('/', (req, res) =&gt; {
    res.sendFile(__dirname + '/index.html');
  });
   
  // upload csv to database
  app.post('/uploadfile', upload.single(&quot;uploadfile&quot;), (req, res) =&gt;{
      UploadCsvDataToMySQL(__dirname + '/uploads/' + req.file.filename);
      console.log('CSV file data has been uploaded in mysql database ' + err);
  });
   
  function UploadCsvDataToMySQL(filePath){
      let stream = fs.createReadStream(filePath);
      let csvData = [];
      let csvStream = csv
          .parse()
          .on(&quot;data&quot;, function (data) {
              csvData.push(data);
          })
          .on(&quot;end&quot;, function () {
              // Remove Header ROW
              csvData.shift();
    
              // Open the MySQL connection
              db.connect((error) =&gt; {
                  if (error) {
                      console.error(error);
                  } else {
                      let query = 'INSERT INTO files (File_ID, File_Name) VALUES ?';
                      db.query(query, [csvData], (error, response) =&gt; {
                          console.log(error || response);
                      });
                  }
              });
               
              // delete file after saving to MySQL database
              // -&gt; you can comment the statement to see the uploaded CSV file.
              fs.unlinkSync(filePath)
          });
    
      stream.pipe(csvStream);
  }
   
  //create connection
  const PORT = process.env.PORT || 8000
  app.listen(PORT, () =&gt; console.log(`Server is running at port ${PORT}`))
  </code></pre>
  <p><strong>in this is error appear in PowerShell after file uploaded :</strong></p>
  <p>ReferenceError: err is not defined
  at C:\Users\DELL\Desktop\Advanced\AdvancedSeE\dbmanager\index.js:79:72
  at Layer.handle [as handle_request] (C:\Users\DELL\Desktop\Advanced\AdvancedSeE\dbmanager\node_modules\express\lib\router\layer.js:95:5)
  at next (C:\Users\DELL\Desktop\Advanced\AdvancedSeE\dbmanager\node_modules\express\lib\router\route.js:137:13)
  at Array. (C:\Users\DELL\Desktop\Advanced\AdvancedSeE\dbmanager\node_modules\multer\lib\make-middleware.js:53:37)
  at listener (C:\Users\DELL\Desktop\Advanced\AdvancedSeE\dbmanager\node_modules\on-finished\index.js:169:15)
  at onFinish (C:\Users\DELL\Desktop\Advanced\AdvancedSeE\dbmanager\node_modules\on-finished\index.js:100:5)
  at callback (C:\Users\DELL\Desktop\Advanced\AdvancedSeE\dbmanager\node_modules\ee-first\index.js:55:10)
  at IncomingMessage.onevent (C:\Users\DELL\Desktop\Advanced\AdvancedSeE\dbmanager\node_modules\ee-first\index.js:93:5)
  at IncomingMessage.emit (node:events:341:22)
  at endReadableNT (node:internal/streams/readable:1294:12)
  ResultSetHeader {
  fieldCount: 0,
  affectedRows: 3,
  insertId: 33333,
  info: 'Records: 3  Duplicates: 0  Warnings: 0',
  serverStatus: 2,
  warningStatus: 0
  }</p>