【问题标题】:Rethrow non-MySQL errors Node.js重新抛出非 MySQL 错误 Node.js
【发布时间】:2019-06-15 01:26:42
【问题描述】:

您好,下面的代码从 mysql 数据库加载单个字段,但是当它到达 result[0].IdUtente 点时,会生成以下错误。我该如何解决?

错误:

 throw err; // Rethrow non-MySQL errors
        ^

TypeError: Cannot read property 'IdUtente' of undefined

代码:

var query = "SELECT IdUtente FROM Utente WHERE Username = " + sql.escape(Username) + " and Password=MD5(" + sql.escape(Password) + ") and Tipo!='Risorsa' ";
var ret=DbConfig.query(query, function (err, result) {
    if (err) {
        console.log("Errore login: " + err);
    }
    else {
        console.log(result[0].IdUtente);
        IdUtente = result[0].IdUtente;

    }

});

【问题讨论】:

  • 我遇到了同样的错误。意识到这是由于 SQL 语法不正确造成的。确保您编写的 SQL 也是正确的。

标签: mysql node.js npm


【解决方案1】:

查询拉取空结果时会发生此错误。您必须首先使用result.length 属性检查结果的长度,只有当结果大于0 时,您必须使用索引结果。 请检查下面的代码。

var query = "SELECT IdUtente FROM Utente WHERE Username = " + sql.escape(Username) + " and Password=MD5(" + sql.escape(Password) + ") and Tipo!='Risorsa' ";
var ret=DbConfig.query(query, function (err, result) {
    if (err) {
        console.log("Errore login: " + err);
    }
    else {
        //check to see if the result is empty
        if(result.length > 0){
            console.log(result[0].IdUtente);
            IdUtente = result[0].IdUtente;
       }
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-18
    • 2018-01-14
    • 2015-03-27
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多