So I am trying to query the date input from the form into a table in my database using nodejs. The code for the entire interaction is given below:
app.post('/register', checkNotAuthenticated, async (req, res) => {
const firstname = req.body.first_name
const lastname = req.body.last_name
const email = req.body.email_id
const hashedPassword = await bcrypt.hash(req.body.password, 10)
try {
res.redirect("/login")
} catch {
res.redirect('/register')
}
const sql = "INSERT INTO users (first_name, last_name, email, password) VALUES ('"+firstname+"', '"+lastname+"','"+email+"', '"+hashedPassword+"')"
con.query(sql, function(err, result) {
if (err) throw err
console.log('New user registered: ' + users)
})
})
Everything else seems to be fine except that when I am registering a user through the register form, the following error is being thrown to the terminal:
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
Connected to mysql database!
/Users/vaidiklapalikar/Desktop/current project/node_modules/mysql/lib/protocol/Parser.js:437
throw err; // Rethrow non-MySQL errors
^
ReferenceError: users is not defined
at Query.<anonymous> (/Users/vaidiklapalikar/Desktop/current project/server.js:83:43)
at Query.<anonymous> (/Users/vaidiklapalikar/Desktop/current project/node_modules/mysql/lib/Connection.js:526:10)
at Query._callback (/Users/vaidiklapalikar/Desktop/current project/node_modules/mysql/lib/Connection.js:488:16)
at Query.Sequence.end (/Users/vaidiklapalikar/Desktop/current project/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
at Query._handleFinalResultPacket (/Users/vaidiklapalikar/Desktop/current project/node_modules/mysql/lib/protocol/sequences/Query.js:149:8)
at Query.OkPacket (/Users/vaidiklapalikar/Desktop/current project/node_modules/mysql/lib/protocol/sequences/Query.js:74:10)
at Protocol._parsePacket (/Users/vaidiklapalikar/Desktop/current project/node_modules/mysql/lib/protocol/Protocol.js:291:23)
at Parser._parsePacket (/Users/vaidiklapalikar/Desktop/current project/node_modules/mysql/lib/protocol/Parser.js:433:10)
at Parser.write (/Users/vaidiklapalikar/Desktop/current project/node_modules/mysql/lib/protocol/Parser.js:43:10)
at Protocol.write (/Users/vaidiklapalikar/Desktop/current project/node_modules/mysql/lib/protocol/Protocol.js:38:16)
[nodemon] app crashed - waiting for file changes before starting...
Please help me out with this. Thanks in advance. P.S. I am a beginner with nodejs and mysql so, if possible, please help me out accordingly as I have no idea about why this error is generated.