jeudi 21 mars 2019

how to do an if statement in javascript to check if a JSON array is empty?

I am trying various answers from stack overflow to write an if statement which check is JSON array is populated or empty. However, console.log is always returning undefined. I am using node.js and webstorm as IDE. can I get some help to solve this?

function selectData(){
    var sql = "SELECT * FROM cust where LastName = '" +registerArray[0]+"'";
    con.query(sql, function (err, result) {
        console.log(JSON.stringify(result)); //correctly displaying stuffs from db
        if (Object.keys(result).length !== 0) { //statement still not working correctly.
            var pw = "SELECT ID FROM cust where LastName = '" +registerArray[0]+"'";
            con.query(pw, function (err, result) {
                var idcheck  = JSON.stringify(result);
                console.log(JSON.stringify(result)); //always shows undefined
                if (idcheck !== registerArray[0]){
                    response.send("{re enter correct password}");
                } else {
                    response.send("{login successfully}");
                }
            });

        } else {
            response.send("{sign up first}");
        }
    });
}

Aucun commentaire:

Enregistrer un commentaire