0

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);

    }

}

1 Answers1

0

Just put this code and monitor on console

const getIdCPS = (ricovero) => {
  try {
    const errorObj = { code: 400, error: 'Wrong Input' }
    if (!ricovero || !ricovero.cps) {
      throw errorObj;
    }
    const querySQL = "SELECT id FROM codici_pronto_soccorso WHERE codice ='" + ricovero.cps + "'";
    connection.query(querySQL, (err, result) => {
      if (err) {
        throw err;
      } else if (result) {
        console.log(result);
        return result;
      } else {
        throw err;
      }
    });
  } catch (err) {
    console.error(err);
    throw err;
  }
};
Neel Rathod
  • 2,013
  • 12
  • 28