如何解决 sql errno: '1064' and ERR_HTTP_HEADERS_SENT 错误?

How to solve the sql errno: '1064' and ERR_HTTP_HEADERS_SENT error?

我有以下代码,其中包含几个 sql 查询:

createNewOrder: (data,callBack) => {
     

        pool.query(
            
            `Insert into ordering (supplycontract) values (?)`,
            [
              data.supplierId
            ],
            (error,results,fields) => {
                if(error){
                   return callBack(error)
                }
                pool.query(
                    `select id from ordering order by id desc limit 1`, 
                    [
                        data.articleId
                    ],
                    (error,results1,fields) =>{
                        if(error){
                            return callBack(error)
                        }
                        let orderid = results1[0].id;
                        console.log(orderid);

                        pool.query(
                            `Insert into position (quantity,delivery,ordering,positioninorder,articlecontract) values(?,?,?,1,?)`,
                            [
                                data.quantity,
                                new Date(),
                                orderid,
                                data.articleId 
                            ],
                            (error,results3,fields) => {
                                if(error){
                                    callBack(error)
                                 }
                                 //callBack(null,results3)
                            }

                        );
                    }

                );
                return callBack(null,results)
            }
        );
    } 

我尝试了几种技巧来改进代码,但不幸的是我收到以下错误:

Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 
'position (quantity,delivery,ordering,positioninorder,articlecontract) values(54,' at line 14
code: 'ER_PARSE_ERROR',
  errno: 1064,
  sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'position (quantity,delivery,ordering,positioninorder,articlecontract) values(54,' at line 1",
  sqlState: '42000',
  index: 0,
  sql: "Insert into position (quantity,delivery,ordering,positioninorder,articlecontract) values(54,'2021-07-11 19:18:39.334',16,1,5)"
}

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:518:11)
 code: 'ERR_HTTP_HEADERS_SENT'

您的代码有多个问题

  1. return callBack(null,results) 应该在第三个查询的回调中。
  2. callBack(error) 在第三个查询的错误处理程序中应该有 return 语句,如 return callBack(error).
  3. SQL 错误:这是因为您使用保留关键字 position 作为您的 table 名称。如果您的 SQL 查询包含保留关键字,您可以使用反引号 "``" 来转义保留关键字。
createNewOrder: (data, callBack) => {
    pool.query(`Insert into ordering (supplycontract) values (?)`, [data.supplierId], (error, results, fields) => {
        if (error) {
            return callBack(error)
        }
        pool.query(`select id from ordering order by id desc limit 1`, [data.articleId], (error, results1, fields) => {
            if (error) {
                return callBack(error)
            }
            let orderid = results1[0].id;
            console.log(orderid);
            pool.query("Insert into `position` (quantity,delivery,ordering,positioninorder,articlecontract) values(?,?,?,1,?)", [data.quantity, new Date(), orderid, data.articleId], (error, results3, fields) => {
                if (error) {
                    return callBack(error)
                }
                return callBack(null, results)
            });
        });
    });
}

一些注意事项

  1. 错误 Cannot set headers after they are sent 当您尝试发送两次响应时出现,简单来说,如果您在一个请求周期中调用 res.json()res.status().json() 两次,您将得到上面的错误。
function myController(req, res) {
  
   res.json({ status: "success" });
   res.json({ status: "success" }); // this will throw the error defined above

}
  1. 要使用保留关键字,请使用反引号。或者更好的是,不要使用保留关键字。