【问题标题】:Inserting form data into mysql using nodejs. Where am I going wrong?使用 nodejs 将表单数据插入 mysql。我哪里错了?
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 对于不使用 brcrypt 函数的其他常量是否也一样? (即 '''const lastname''' 和 '''const email'''?
    • 所以我将常量从 try catch 块中取出,现在它说 hashesPass in 未定义。这怎么可能? (给出未处理的承诺拒绝错误警告)
    猜你喜欢
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 2012-08-17
    • 1970-01-01
    相关资源
    最近更新 更多