I'm working on a log in form with node.js and mysql as the database. When I run the programm and try to register a new user I get this error when I press submit.
When I press submit I get the folowing error in the console in the inspect page in chrome: error and I get in the terminal inside vs code the following error:
node:internal/process/promises:288
triggerUncaughtException(err, true /* fromPromise */);
^
Error: ER_EMPTY_QUERY: Query was empty
at Sequence._packetToError (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Query.ErrorPacket (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\mysql\lib\protocol\sequences\Query.js:79:18)
at Protocol._parsePacket (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\mysql\lib\Connection.js:88:28)
at Socket.<anonymous> (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\mysql\lib\Connection.js:526:10)
at Socket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:324:12)
--------------------
at Protocol._enqueue (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Connection.query (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\mysql\lib\Connection.js:198:25)
at register (D:\programmeer stuff\javascript\informatica site\New folder\controllers\register.js:9:12)
at Layer.handle [as handle_request] (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\express\lib\router\layer.js:95:5)
at next (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\express\lib\router\layer.js:95:5)
at D:\programmeer stuff\javascript\informatica site\New folder\node_modules\express\lib\router\index.js:284:15
at Function.process_params (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\express\lib\router\index.js:346:12)
at next (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\express\lib\router\index.js:280:10) {
code: 'ER_EMPTY_QUERY',
errno: 1065,
sqlMessage: 'Query was empty',
sqlState: '42000',
index: 0,
sql: undefined
}
The code I try to run is:
controllers/register.js
const db = require("../routes/db-config");
const bcrypt =require("bcryptjs")
const register = async (req, res) => {
const {email, password: Npassword} = req.body
if(!email || !Npassword) return res.json({satus:'error', error:"Please enter your email and password"});
else{
console.log(email);
db.query('SELECT email FROM users WHERE email = ?'
, async(err, result) => {
if(err) throw err;
if(result[0])return res.json({satus:'error', error:"email already in use"})
else {
const password = bcrypt.hash(Npassword, 8);
console.log(password)
db.query('INSERT INTO users SET ?', {email: email, password: password}, (error, results)=>{
if (error) throw error;
return res.json({ satus:"success", success:"account registred succesfully" })
})
}
})
}
}
module.exports = register;
I think either db.query('INSERT INTO users SET ?', {email: email, password: password}, (error, results)=> or `db.query('SELECT email FROM users WHERE email = ?'