1
我使用mysql和nodeJS並創建查詢。我爲查詢得到的行是'未定義的'。這從來沒有發生過我,我很難過。NodeJS MySQL查詢給出'未定義'行
// 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();
}
});
你確定它確實是'console.log(rows);'打印'undefined'嗎?這是沒有道理的,因爲那麼'rows.length'會拋出一個_cannot讀屬性長度爲undefined_的錯誤。 –
哦,我稍後修改了代碼。是的,我首先得到「無法讀取屬性長度未定義」的錯誤 –
你確定'console.log(error);'沒有記錄錯誤。你應該將下面的所有代碼包裝在if(error){console.log(error); }'在'if(error)'的'else'塊中。 –