2016-09-30 93 views
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(); 
    } 
}); 
+1

你确定它确实是'console.log(rows);'打印'undefined'吗?这是没有道理的,因为那么'rows.length'会抛出一个_cannot读属性长度为undefined_的错误。 –

+0

哦,我稍后修改了代码。是的,我首先得到“无法读取属性长度未定义”的错误 –

+0

你确定'console.log(error);'没有记录错误。你应该将下面的所有代码包装在if(error){console.log(error); }'在'if(error)'的'else'块中。 –

回答

1

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