繁体   English   中英

使用 nodejs 将表单数据插入 mysql。 我哪里错了?

[英]Inserting form data into mysql using nodejs. Where am I going wrong?

所以我正在为我的学校开发一个网络应用程序,我应该在其中创建一个登录注册页面。 为此,我决定使用 node、express 和 mysql。 我刚开始学习node,是个新手。 下面给出的是我为将注册表中的数据插入到 mysql 数据库表中而编写的代码(我之前已经成功连接到数据库):

app.post('/register', async (req, res) => {
    try {
        const firstname = req.body.first_name
        const lastname = req.body.last_name
        const email = req.body.email_id
        const hashedPass = await bcrypt.hash(req.body.password, 10)
        res.redirect("/login")
    } catch {
        res.redirect('/register')
    }
     const sql = "INSERT INTO users (first_name, last_name, email, password) VALUES ('"+firstname+"', '"+lastname+"','"+email+"', '"+hashedPass+"')"
        con.query(sql, function(err, result) {
            if (err) throw err
        console.log('New user registered: ' + users)
        })
})
app.listen(3000)

但是,当我使用 nodemon 刷新服务器并将测试数据输入表单以检查用户是否已注册时,终端上显示以下错误:

Connected to mysql database!
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
Connected to mysql database!
(node:60021) UnhandledPromiseRejectionWarning: ReferenceError: firstname is not defined
    at /Users/vaidiklapalikar/Desktop/current project/server.js:55:92
(node:60021) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:60021) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Connected to mysql database!
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
Connected to mysql database!
(node:60021) UnhandledPromiseRejectionWarning: ReferenceError: firstname is not defined
    at /Users/vaidiklapalikar/Desktop/current project/server.js:55:92
(node:60021) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:60021) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我不知道发生了什么。 这个你能帮我吗! 提前致谢。

const firstname的范围是try块,但您尝试在块之外读取它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM