import a CSV file to MySQL [duplicate] - javascript

This question already has answers here:
using (-) dash in mysql table name
(2 answers)
Closed 1 year ago.
I'm a beginner in nodejs and I'm trying to import a CSV file to MySQL
the connect work successfully but when I upload the file it gives an error
I do this :
//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');
});
//#type POST
// 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}`))
but it's given me this error:
ReferenceError: err is not defined
at C:\Users\DELL\Desktop\Advanced\AdvancedSeE\dbmanager\index.js:80: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)
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-ID, File_Name) VALUES ('112', 'test2')' at line 1
at Packet.asError (C:\Users\DELL\Desktop\Advanced\AdvancedSeE\dbmanager\node_modules\mysql2\lib\packets\packet.js:728:17)
at Query.execute (C:\Users\DELL\Desktop\Advanced\AdvancedSeE\dbmanager\node_modules\mysql2\lib\commands\command.js:29:26)
at Connection.handlePacket (C:\Users\DELL\Desktop\Advanced\AdvancedSeE\dbmanager\node_modules\mysql2\lib\connection.js:456:32)
at PacketParser.onPacket (C:\Users\DELL\Desktop\Advanced\AdvancedSeE\dbmanager\node_modules\mysql2\lib\connection.js:85:12)
at PacketParser.executeStart (C:\Users\DELL\Desktop\Advanced\AdvancedSeE\dbmanager\node_modules\mysql2\lib\packet_parser.js:75:16)
at Socket. (C:\Users\DELL\Desktop\Advanced\AdvancedSeE\dbmanager\node_modules\mysql2\lib\connection.js:92:25)
at Socket.emit (node:events:329:20)
at addChunk (node:internal/streams/readable:304:12)
at readableAddChunk (node:internal/streams/readable:279:9)
at Socket.Readable.push (node:internal/streams/readable:218:10) {
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-ID, File_Name) VALUES ('112', 'test2')' at line 1",
sql: "INSERT INTO files (File-ID, File_Name) VALUES ('112', 'test2')"
}

Seems that a column name in you import files is not valid as you see at the end of you error message you have
INSERT INTO files (File-ID, File_Name) VALUES ('112', 'test2')
The column name File-ID should be changed (the minus sign in mysql is used for math and mean column FIle - column ID )
or using backtics
INSERT INTO files (`File-ID`, File_Name) VALUES ('112', 'test2')
or avoidng the minus sign
INSERT INTO files (File_ID, File_Name) VALUES ('112', 'test2')
try edit the file and change the name properly

Related

MulterError: Unexpected field at wrappedFileFilter

I am trying to upload a file to S3 on Amazon by creating an API,,, however, I am having issues while uploading the file.
The API has an issue when I select the file using postman I get the error below
I don't get the source of the issue
MulterError: Unexpected field
at wrappedFileFilter (E:\MyProjects\reapmgtapi\node_modules\multer\index.js:40:19)
at Busboy.<anonymous> (E:\MyProjects\reapmgtapi\node_modules\multer\lib\make-middleware.js:114:7)
at Busboy.emit (events.js:198:13)
at Busboy.EventEmitter.emit (domain.js:448:20)
at Busboy.emit (E:\MyProjects\reapmgtapi\node_modules\busboy\lib\main.js:38:33)
at PartStream.<anonymous> (E:\MyProjects\reapmgtapi\node_modules\busboy\lib\types\multipart.js:213:13)
at PartStream.emit (events.js:198:13)
at PartStream.EventEmitter.emit (domain.js:448:20)
at HeaderParser.<anonymous> (E:\MyProjects\reapmgtapi\node_modules\dicer\lib\Dicer.js:51:16)
at HeaderParser.emit (events.js:198:13)
Here are my project files
multer.config.js
const multer = require('multer');
var storage = multer.memoryStorage()
var upload = multer({ storage: storage });
module.exports = upload;
controller.js
const s3 = require('../config/s3.config');
const env = require('../config/s3.env');
exports.doUpload = (req, res) => {
const params = {
Bucket: env.Bucket,
Key: req.file.originalname,
Body: req.file.buffer
}
s3.upload(params, (err, data) => {
if (err) {
res.status(500).send("Error is as follows -> " + err);
}
res.send("File uploaded successfully! -> keyname = " + req.file.originalname);
});
}
s3.router.js
let express = require('express');
let router = express.Router();
let upload = require('../config/multer.config');
const awsWorker = require('../controllers/controller');
router.post('/api/files/upload', upload.single("file"), awsWorker.doUpload);
module.exports = router;
the code is fine,,, I just had to create a request with a file with "file" as the key and the file input as the parameter... I was trying to input the originalname and buffer parameters which should be extracted from the file

Node.js POST returns, cannot post to Database table

I just started Backend in Node.js
Now i want to write a REST api in Node.js that is supposed to send Data into a Mysql Database. The GET request works fine apparently, so there are no Issues whatsoever with the GET Request, just the POST, which does not save information into the Database.
My Code is Looking Like this
app.post('/users/', function(req,res){
let sql ="insert into personal_info (name, email, gender, national_id,salary) values (?)";
let values = [req.body.name, req.body.email, req.body.gender,req.body.national_id,req.body.salary];
db.query(sql, [values], function(err, data, fields){
if (err) throw err;
res.json({
status: 200,
message: "New user added successfully"
})
});
});
Running it inside POSTman i am getting this :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /users</pre>
</body>
</html>
Is there something i appear to be Missing? Like I said, I am very new to this, and I do need clarification of some sort.
Edits
Code Looks Like this Now :
app.post('/api/user/', function(req,res){
let sql ="insert into personal_info (name, email, gender, national_id,salary) values (?,?,?,?,?)";
let values = [req.body.name, req.body.email, req.body.gender,req.body.national_id,req.body.salary];
dbcon.query(sql, [values], function(err, data, fields){
if (err) throw err;
res.json({
status: 200,
message: "New user added successfully"
})
});
});
And here is the Error
D:\Node_JSREST\nodejsrest_example>node index.js
Node app is on port 5900
D:\Node_JSREST\nodejsrest_example\node_modules\mysql\lib\protocol\Parser.js:437
throw err; // Rethrow non-MySQL errors
^
Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?,?,?,?)' at line 1
at Query.Sequence._packetToError (D:\Node_JSREST\nodejsrest_example\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Query.ErrorPacket (D:\Node_JSREST\nodejsrest_example\node_modules\mysql\lib\protocol\sequences\Query.js:79:18)
at Protocol._parsePacket (D:\Node_JSREST\nodejsrest_example\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (D:\Node_JSREST\nodejsrest_example\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (D:\Node_JSREST\nodejsrest_example\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (D:\Node_JSREST\nodejsrest_example\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (D:\Node_JSREST\nodejsrest_example\node_modules\mysql\lib\Connection.js:88:28)
at Socket.<anonymous> (D:\Node_JSREST\nodejsrest_example\node_modules\mysql\lib\Connection.js:526:10)
at Socket.emit (events.js:189:13)
at addChunk (_stream_readable.js:284:12)
--------------------
at Protocol._enqueue (D:\Node_JSREST\nodejsrest_example\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Connection.query (D:\Node_JSREST\nodejsrest_example\node_modules\mysql\lib\Connection.js:198:25)
at D:\Node_JSREST\nodejsrest_example\index.js:42:9
at Layer.handle [as handle_request] (D:\Node_JSREST\nodejsrest_example\node_modules\express\lib\router\layer.js:95:5)
at next (D:\Node_JSREST\nodejsrest_example\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\Node_JSREST\nodejsrest_example\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\Node_JSREST\nodejsrest_example\node_modules\express\lib\router\layer.js:95:5)
at D:\Node_JSREST\nodejsrest_example\node_modules\express\lib\router\index.js:281:22
at Function.process_params (D:\Node_JSREST\nodejsrest_example\node_modules\express\lib\router\index.js:335:12)
at next (D:\Node_JSREST\nodejsrest_example\node_modules\express\lib\router\index.js:275:10)
D:\Node_JSREST\nodejsrest_example>
Edits Again
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mysql = require('mysql');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.get('/',function(req,res){
return res.send({error:true, message : 'hello world'});
});
var dbcon = mysql.createConnection({host:'localhost',user:'root',password:'',database:'staffdb' });
dbcon.connect();
app.get('/users',function(req,res){
dbcon.query('select * from personal_info', function (error, results, fields){
if (error) throw error;
return res.send({ error: false, data: results, message: 'users list.' });
});
});
app.get('/users/:id',function (req, res){
let user_id = req.params.id;
if (!user_id) {
return res.status(400).send({ error: true, message: 'Please provide user_id' });
}
dbcon.query('select * from personal_info where id=?',user_id,function(error, results,fields){
if (error) throw error;
return res.send({ error: false, data: results[0], message: 'users list.' });
});
});
app.post('/api/user/', function(req,res){
let sql ="insert into personal_info (name, email, gender, national_id,salary) values (?)";
let values = [req.body.name, req.body.email, req.body.gender,req.body.national_id,req.body.salary];
dbcon.query(sql, [values], function(err, data, fields){
if (error) throw error;
res.json({
status: 200,
message: "New user added successfully"
})
});
});
app.listen(5900,function(){
console.log('Node app is on port 5900');
});
module.exports = app;
I think your using the wrong node module for your database engine. Here you import the mysql:
var mysql = require('mysql');
But in the error we can see that you have an error for a mariaDB server:
Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?,?,?,?)' at line 1
I suggest you to try with the mariadb node module. Here is an example from mariadb.com. The README is also good.

my express endpoint route with mysql logs an error: connect ECONNREFUSED 127.0.0.1

I'm building a web app for a bootcamp. I'm using express and mysql. I'm routing a get request to an endpoint. The route should query my mysql database table to select all. I'm expecting to send the result to the chrome page. Instead I get this error:
Error: connect ECONNREFUSED 127.0.0.1:8211
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1054:14)
--------------------
at Protocol._enqueue (/Users/cnebs/Documents/HRATX/hratx42-fullstack-review/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/Users/cnebs/Documents/HRATX/hratx42-fullstack-review/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/Users/cnebs/Documents/HRATX/hratx42-fullstack-review/node_modules/mysql/lib/Connection.js:119:18)
at Connection._implyConnect (/Users/cnebs/Documents/HRATX/hratx42-fullstack-review/node_modules/mysql/lib/Connection.js:457:10)
at Connection.query (/Users/cnebs/Documents/HRATX/hratx42-fullstack-review/node_modules/mysql/lib/Connection.js:199:8)
at Object.getAllUsers (/Users/cnebs/Documents/HRATX/hratx42-fullstack-review/database/index.js:17:14)
at /Users/cnebs/Documents/HRATX/hratx42-fullstack-review/server/index.js:22:6
at Layer.handle [as handle_request] (/Users/cnebs/Documents/HRATX/hratx42-fullstack-review/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/cnebs/Documents/HRATX/hratx42-fullstack-review/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/cnebs/Documents/HRATX/hratx42-fullstack-review/node_modules/express/lib/router/route.js:112:3) {
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 8211,
fatal: true
}
I'm able to query the database in terminal. I'm able to send text to the page on chrome through express. I've built a database and table. I haven't used the api for my project to populate the table yet. My console is logging the error through a function in my database index which I've imported to express to be used in the get route.
Here is the database index:
const mysql = require('mysql');
const connection = mysql.createConnection({
host: "127.0.0.1",
user: "root",
password: "password",
database: "github",
port: 8211
});
const test = () => {
connection.query("DESCRIBE git_repos", (err, res) => {
console.log('selection: ', res)
})
}
const getAllUsers = cb => {
connection.query(`select * from todos`, (err, res) => {
if (err) {
console.log("error in getAllUsers: ", err);
cb(err);
} else {
cb(null, res);
}
});
}
module.exports = { test, getAllUsers }
Here is the server index:
const express = require('express');
const db = require('../database')
let app = express();
app.use(express.static(__dirname + '/../client/dist'));
app.get('/repos', function (req, res) {
// TODO - your code here!
// This route should send back the top 25 repos
db.getAllUsers((err, result) => {
if (err) {
console.error(err)
res.status(404).end();
} else {
console.log('Getting')
res.send(result)
}
})
});
let port = 1128;
app.listen(port, function() {
console.log(`listening on port ${port}`);
});
I expect to see any result at all from my query delivered to the page from the res.send.
Instead, I see that the localhost page can't be found and the err response above in my server terminal.
You aren't able to directly choose the port for your database server in the createConnection(). Remove the port property and it will function.

How to fix "MulterError: Unexpected field" in a nodejs express server?

I'm setting up a server to upload zip files from the client. The server runs with express and multer to do this. When uploading a file, the server throws a "MulterError: Unexpected field" error and I cannot figuere out what is cousing it.
I've tried with png images and It works just fine. But with the zip files it just doesn't work.
const multerConfig = {
//specify diskStorage (another option is memory)
storage: multer.diskStorage({
//specify destination
destination: function(req, file, next){
next(null, './public/zip-storage');
},
//specify the filename to be unique
filename: function(req, file, next){
console.log(file);
const ext = file.mimetype.split('/')[1];
//set the file fieldname to a unique name containing the original name, current datetime and the extension.
next(null, file.fieldname + '-' + Date.now() + '.'+ext);
}
}),
// filter out and prevent non-image files.
fileFilter: function(req, file, next){
if(!file){
next();
}
// only permit zip mimetypes
const zip = file.mimetype.startsWith('application');
if(zip){
console.log('zip uploaded');
next(null, true);
}else{
console.log("file not supported")
errorReq = true;
return next();
}
}
};
/* ROUTES
**********/
app.get('/', function(req, res){
res.render('index.html');
});
var errorDetection = function(){
if(!errorReq){
errorReq = false;
return('complete.html');
} else{
errorReq = false;
return('errorupload.html');
}
}
app.post('/upload', multer(multerConfig).single('photo'),function(req, res){
//Here is where I could add functions to then get the url of the new photo
//And relocate that to a cloud storage solution with a callback containing its new url
//then ideally loading that into your database solution. Use case - user uploading an avatar...
res.redirect('complete.html');
}
);
// RUN SERVER
app.listen(port,function(){
console.log(`Server listening on port ${port}`);
});
This is the error:
MulterError: Unexpected field
at wrappedFileFilter (/home/axentiva-miguel/Documentos/ServerAPP/node_modules/multer/index.js:40:19)
at Busboy.<anonymous> (/home/axentiva-miguel/Documentos/ServerAPP/node_modules/multer/lib/make-middleware.js:114:7)
at emitMany (events.js:147:13)
at Busboy.emit (events.js:224:7)
at Busboy.emit (/home/axentiva-miguel/Documentos/ServerAPP/node_modules/busboy/lib/main.js:38:33)
at PartStream.<anonymous> (/home/axentiva-miguel/Documentos/ServerAPP/node_modules/busboy/lib/types/multipart.js:213:13)
at emitOne (events.js:116:13)
at PartStream.emit (events.js:211:7)
at HeaderParser.<anonymous> (/home/axentiva-miguel/Documentos/ServerAPP/node_modules/dicer/lib/Dicer.js:51:16)
at emitOne (events.js:116:13)
In the code
app.post('/upload', multer(multerConfig).single('photo'),function(req, res){
//Here is where I could add functions to then get the URL of the new photo
//And relocate that to a cloud storage solution with a callback containing its new URL
//then ideally loading that into your database solution. Use case - user uploading an avatar...
res.redirect('complete.html');
}
I was putting the value single('photo'), when in the HTML form I sent, a 'zip' value in there. Changing that made the code work as expected :)

Node js mysql query gives error when executed second time

I am trying to make a REST api in node using express. When i open the url in browser the first time, it runs fine and gives me the correct output. But when I hit the api url the second time, the app crashes with the error :
events.js:141
throw er; // Unhandled 'error' event
^
Error: Cannot enqueue Handshake after invoking quit.
This is the code I'm using :
var express = require('express');
var mysql = require('mysql');
var app = express();
var connection = mysql.createConnection({
host: 'localhost',
user: 'USER_NAME',
password: 'PASSWORD'
});
app.use(express.static(__dirname + '/public'));
var port = 3333;
app.get('/api/users/:user_id', function(req, res){
var lead_id = req.params.user_id;
connection.connect();
connection.query('use DB_NAME;', function (err) {
if(err) throw err;
connection.query('select * from users where user_id = ' + user_id, function (err, rows) {
if(err) throw err;
res.json(rows);
connection.end();
});
});
});
app.listen(port);
console.log("The app is running on port " + port);
Can someone tell me what am I doing wrong ?
Just remove connection.connect() and connection.end(). Then it should work.
connection.connect() should be called once or none. Because connection.query will connect I'd it not connected.
PS - connection.connect() need to be called when the connection is lost.
Try this to remove the redudant connection. This was the first result of a google search of the error message - always try to google error messages before asking SO.

Categories

Resources