Home > Enterprise >  import CSV file into mysql database using nodejs
import CSV file into mysql database using nodejs

Time:11-19

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 :

//use express static folder
app.use(express.static("./public"))
 
// 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) => {
        callBack(null, './uploads/')    
    },
    filename: (req, file, callBack) => {
        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) => {
  res.sendFile(__dirname   '/index.html');
});
 
// upload csv to database
app.post('/uploadfile', upload.single("uploadfile"), (req, res) =>{
    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("data", function (data) {
            csvData.push(data);
        })
        .on("end", function () {
            // Remove Header ROW
            csvData.shift();
  
            // Open the MySQL connection
            db.connect((error) => {
                if (error) {
                    console.error(error);
                } else {
                    let query = 'INSERT INTO files (File_ID, File_Name) VALUES ?';
                    db.query(query, [csvData], (error, response) => {
                        console.log(error || response);
                    });
                }
            });
             
            // delete file after saving to MySQL database
            // -> 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, () => console.log(`Server is running at port ${PORT}`))

in this is error appear in PowerShell after file uploaded :

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 }

CodePudding user response:

In this following area you added err in console log which is not defined in the file

// upload csv to database
app.post('/uploadfile', upload.single("uploadfile"), (req, res) =>{
   UploadCsvDataToMySQL(__dirname   '/uploads/'   req.file.filename);
   console.log('CSV file data has been uploaded in mysql database '   err); //<----- here err is not defined 
});

just remove this and try if the problem is solved

  • Related