【发布时间】:2020-04-02 15:47:49
【问题描述】:
所以我正在为我的学校开发一个网络应用程序,我应该在其中创建一个登录注册页面。我决定为此使用 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.
我不知道发生了什么。这个你能帮我吗!提前致谢。
【问题讨论】:
标签: javascript mysql node.js express