I am using Node.js and multer to upload a zipped file and save it to MySql database. I have succeeded in creating a login function whereby users can register, login, maintain a session key, and all is saved to the MySql DB.
When I try to upload the zip file and post it to my route and query it to the database, it doesnt work and says the upload object is undefined.
EDIT: The zipped file uploads and saves to the /upload folder as defined in the storage path in the multer function I added below, so it is working that far. I believe the problem could be in the post request in the JS code above the upload form, and/or in the async function.
EDIT:
//this is the error I receive, however i do not believe there is an issue with the db query itself, I believe it's an issue with the req.body object defined in the async function.
code: 'ER_PARSE_ERROR',
errno: 1064,
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 \'\'undefined\', \'undefined\', \'undefined\'\' at line 1',
sqlState: '42000',
index: 0,
sql:
'INSERT into user_v_file (user_vile,vile_name,vile_desc) VALUES \'undefined\', \'undefined\', \'undefined\'' }
//This is the upload form and post request:
<script> $(document).ready(function() { $("#upload").click(function() { $("#form-upload").toggle(); }); $('#uploadVr').click(function(e){ $.post("http://localhost:3000/upload",{ user_vile : $("#uservile").val(), vile_name : $("#vilename").val(), vile_desc : $("#viledesc").val() },function(data){ if(!data.error) { window.location.href = "/home"; } else { alert(data.message); } }); }) }); </script> </head> <body> <button type="button" id="upload" value="Upload">Upload</button> <button onclick="window.location.href='/home.html'" type="button" id="explore" value="Explore">Explore</button> <form id="form-upload" action="/upload" method="POST" enctype="multipart/form-data" style="display:none"> Upload folder: <input type="file" id="uservile" name="vr_files"> VR name: <input type="text" id="vilename"> VR description: <input type="text" id="viledesc"> <input type="submit" id="uploadVr " value="Uploadfiles"/> </form> </body>
// this is async function
const upload = async (req, res) => { try { const {vile_name, vile_desc} = req.body; const {user_vile} = req.file; const resp = await db.upload(user_vile, vile_name, vile_desc); //should it be vr_file..? req.session.key = resp; res.send({error: false, message: "Vile added"}); } catch (e) { res.send({error: true, message: "Vile upload failed"}) console.log(e); } console.log(req.body); };
//this is the multer function to define upload storage path
var storagePath = multer.diskStorage({ destination: function (req, file, cb) { cb(null, './files/uploads') }, filename: function (req, file, cb) { cb(null, file.fieldname + '-' + Date.now()) } }) var upload = multer({storage: storagePath})
// this is the route
router.post('/upload', upload.single('user_vile'), main.upload);
//this is the DB query
const upload = async (user_vile, vile_name, vile_desc) => {
const results = await testDb.query(`INSERT into user_v_file (user_vile,vile_name,vile_desc) VALUES '${user_vile}', '${vile_name}', '${vile_desc}'`);
return results
};