【问题标题】:Making an consult with javascript and nodejs with mysql librarie使用 mysql 库咨询 javascript 和 nodejs
【发布时间】:2020-08-07 14:45:38
【问题描述】:

我的代码中有下一个咨询

class FirstloginController {


    async getToken(req, res, next) {
        const { token } = req.params;
        const {access_token} = req.query;

        try {
            const decoded = jwt.verify(access_token, process.env.JWT_ACCOUNT_ACTIVATION)
            res.json({ message: "El acces token es valido", email: decoded})
            const email_consulted = res.json({ email: decoded})
            let select = await pool.query(`SELECT User_email FROM user WHERE User_email=${email_consulted}`)
            if (select != "") {
            const result = await pool.query(`INSERT INTO user (User_email) VALUES (${email_consulted})`);

            }
    
       } catch (err) {
           next(err);
       }
    }

}

由于某种原因,它在插入时无法正常工作,并且在选择中我遇到了下一个错误: (节点:14440)UnhandledPromiseRejectionWarning:错误:ER_BAD_FIELD_ERROR:'where 子句'中的未知列'undefined'

GET /firstlogin?access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InJleWVzZUBncnVwb2ludmVyby5jb20iLCJpYXQiOjE1OTY3NTE1NDQsImV4cCI6MTU5NjgzNzk0NH0.ZefITNCD7cQ6ZvDMHiTEf4yc9UQI8BKDf9-839agDno 200 12.143 ms - 115
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

我不知道为什么会这样,但一切看起来都很好,¿你发现有什么问题吗?

【问题讨论】:

  • 不确定,但我认为您至少应该将await 用于pool.query()
  • @Rashomon 好的,我更改了我的代码,我将在一秒钟内更新,现在我收到下一个错误:错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头

标签: javascript mysql node.js


【解决方案1】:

您正在向客户端 (res.json()) 发送两个响应,这是不可能的。使第一个仅在令牌无效时执行:

class FirstloginController {
    async getToken(req, res, next) {
        const { token } = req.params;
        const {access_token} = req.query;
        let decoded
        try {
            decoded = jwt.verify(access_token, process.env.JWT_ACCOUNT_ACTIVATION)
        } catch (err) {
            res.json({ message: "El acces token es valido", email: decoded})
            return // dont continue the function if token is not valid
        }
        try {
            const email_consulted = res.json({ email: decoded}) // you are returning a success response but you havent finished the process yet. Usually the success is sent on the end of the process
            let select = await pool.query(`SELECT User_email FROM user WHERE User_email=${email_consulted}`)
            if (select != "") {
                const result = await pool.query(`INSERT INTO user (User_email) VALUES (${email_consulted})`);
            }
       } catch (err) {
           next(err);
       }
    }
}

【讨论】:

    猜你喜欢
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 2022-07-27
    相关资源
    最近更新 更多