NodeJS MySQL查询给出'未定义'的行

我正在使用mysql和nodeJS并创build一个查询。 我得到的查询行是'未定义'。 这从来没有发生过我,我很难过。

// check if URL already exists in database connection.query("SELECT * FROM shorturl WHERE urlsource='?'", [url], function(error, rows, fields){ if (error) { console.log(error); } if (rows.length == 0) { console.log(rows); //prints 'undefined' insertURL(); } else { console.log("creating new entry"); printURL(); } }); 

你可以试试这个,查询连接的结果NodeJS是数组。 尝试添加行[0]希望这会有所帮助。

 if (rows[0].length == 0) { console.log(rows[0]); insertURL(); } else { console.log("creating new entry"); printURL(); }