简体   繁体   English

NodeJS 和 MySQL:SQL 语法错误导致 UnhandledPromiseRejectionWarning

[英]NodeJS and MySQL: Error in SQL syntax leading to UnhandledPromiseRejectionWarning

I am querying a database using NodeJS and MySQL and async functions.我正在使用 NodeJS 和 MySQL 和异步函数查询数据库。 I have the following query in my async function:我的异步 function 中有以下查询:

async function get_data(sample) {

let pool, con;
try {
    pool = await mysql.createPool(config.databaseOptions);
    con = await pool.getConnection();
} catch (err) {
    console.error('Could not connect to database');
    return false;
}

var qry = "SELECT " +
"   t1.sample_id, t1.v_id, t1.s_type as type, t1.sf, " +
"   t1.ef, CONCAT(t1.chr_bkpt1, ':', t1.pos_bkpt1) as b1, " +
"   CONCAT(t1.chr_bkpt2, ':', t1.pos_bkpt2) as b2, " +
"   t1.sge, t1.ege, t1.pscore as som_score, " +
"   t1.wgs, t1.frame, t1.platform, t1.rconf, t1.report, " +
"   t1.target, t1.pclass, t1.evidence, t2.summary, t3.comments " +
"   FROM " +
"   ( " +
"       SELECT s.* " +
"       FROM sample_som s " +
"       WHERE sample_id=? " +
"   ) as t1 " +
"   LEFT JOIN " +
"   ( " +
"       SELECT v_id, c.sample_id, " +
"       CONCAT(c.sample_id,'(',e.disease_type,'): ',c.cur_summary) as summary " +
"       FROM samples e " +
"       LEFT JOIN sample_som c " +
"       ON e.sample_id=c.sample_id " +
"       WHERE c.cur_summary is not null " +
"       AND c.sample_id=? " +
"   ) as t2 " +
"   ON t1.v_id=t2.v_id " +
"   LEFT JOIN ( " +
"       SELECT v_id, c.sample_id, " +
"       CONCAT(c.sample_id,'(',e.disease_type,'): ',c.comments) as comments " +
"       FROM samples e " +
"       LEFT JOIN sample_som c " +
"       ON e.sample_id=c.sample_id " +
"       WHERE c.comments is not null " +
"       AND c.sample_id=? " +
"   ) as t3 " +
"   ON t1.v_id=t3.v_id " +
"   ORDER BY t1.v_id";

const [rows,fields] = await con.query(qry, sample);
await con.release();
return rows;
}

However, I am getting the following error when I run the server:但是,当我运行服务器时出现以下错误:

UnhandledPromiseRejectionWarning: 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 '?)    as t2    ON t1.v_id=t2.v_id    LEFT JOIN (        SELECT v_id' at line 1

I am not sure why I am getting this error and how to resolve this.我不确定为什么会收到此错误以及如何解决此错误。 Any insights are appreciated.任何见解都值得赞赏。

tl;dr Your sample array isn't long enough. tl; dr您的sample数组不够长。

This is one of those cases where a careful reading of the error message will identify your problem.这是仔细阅读错误消息将识别您的问题的情况之一。 MySQL could not parse your query. MySQL 无法解析您的查询。 The first character it did not understand was your second ?它不明白的第一个字符是你的第二个? placeholder.占位符。

You have three ?你有三个? placeholders in your query, and you have offered the sample parameter in your call to con.query(qry, sample) .查询中的占位符,并且您在调用con.query(qry, sample)时提供了sample参数。 That parameter must be an array with the same number of elements as you have placeholders.该参数必须是具有与占位符相同数量的元素的数组。

You have not provided a catch handler around your query operation, so node is throwing the UnhandledPromiseRejectionWarning when your query fails.您没有围绕查询操作提供catch处理程序,因此当查询失败时节点会抛出 UnhandledPromiseRejectionWarning。 Pro tip : always catch and handle errors from DBMSs.专业提示:始终捕获和处理来自 DBMS 的错误。

You can use multiline string constants to hold long queries.您可以使用多行字符串常量来保存长查询。 For example例如

const qry = `
SELECT a,b
  FROM c
 WHERE id = ?
 ORDER BY b;`;

Pro tip: These are much easier to read and edit than the style you used in your example, especially for complex queries such as yours.专业提示:这些比您在示例中使用的样式更容易阅读和编辑,尤其是对于像您这样的复杂查询。

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

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