0

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.

  • 1
    Have a look at line 83 of your `server.js` file. Do you have a variable `users` there? If so, what is its purpose and why is it undefined? – dusthaines Apr 02 '20 at 21:07
  • Thanks a ton @dusthaines for making me understand. I was calling the wrong variable. Instead of 'users' I should have concatenated 'sql', and with that my purpose of displaying the registered user in the bas terminal is also fulfilled! – DoubleAA Apr 02 '20 at 21:26
  • Please try to use **prepared statements** See https://stackoverflow.com/questions/15778572/preventing-sql-injection-in-node-js – nbk Apr 02 '20 at 21:50
  • Happy to help guide you in the right direction. It looks like what you're trying to do is see the info of the user you just inserted. The `mysql` node package will not return the full record on success (you can accomplish this if you also use `Knex`) but it will return the `id` for the newly inserted record. You can access that via your `result` variable in the `query` function at: `result.insertId`. Keep in mind if you insert more than one record with a single request, you will only get back the ID of the last record inserted by the query. – dusthaines Apr 03 '20 at 13:26

0 Answers0