mercredi 21 août 2019

cannot read property of undefined with result of mysql

I have this problem, although I managed the value of "result", the conditional expression "if" evaluates "result" always as "! == undefined" I also tried to manage with "result! == ''" but not handles it correctly. In this case I have no results from the sql query because "ricovero.cps" is not in the database and so I wrote some code to handle this case. How should I behave in order for the "if" to work correctly?

function getIdCPS(ricovero){

    console.log("getIdCPS()");
    querySQL = "SELECT id FROM codici_pronto_soccorso WHERE codice ='"+ricovero.cps+"'";
    console.log("querySQL="+querySQL);

    try{

        connection.query(querySQL, function(err, result) {

            if(err)
                console.log(err);

            if( result === undefined){

                return "";
            }else
                console.log("result is defined");
                console.log("result=("+result+")");
                return result[0].id;

        });

    }catch(e){

        console.log("try/catch error:" + e);

    }

}

Aucun commentaire:

Enregistrer un commentaire