Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if object is empty doesn't work

I am testing the case if my object was empty, so intentionally I made a query that has no match in the database, which should evaluate my else if statement however I don't get any response in the console.

What am I doing wrong?

user.loginUser = (jUserData, res) => {
    var aData = [
        jUserData.email,
        jUserData.mobile_number,
        0
    ]
    var sQuery = 'SELECT * FROM users WHERE email = ? AND mobile_number = ? AND active = ?'

    function isEmpty(obj) {
        for (var key in obj) {
            if (obj.hasOwnProperty(key))
                return false;
        }
        return true;
    }
    db.each(sQuery, aData, function (err, jRow) {
        console.log(jRow)
        if (err) {
            console.log('BAD, user not logged in')
            return res(true, {
                status: "INTERNAL SERVER ERROR"
            })
        }
        if (isEmpty(jRow)) {
            console.log('NOT FOUND')
            return res(true, {
                status: "NOT FOUND"
            })
        }
        console.log('GREAT, user logged in')
        return res(false, jRow)
        console.log(jRow)
    })
}
like image 755
July333 Avatar asked May 07 '18 09:05

July333


1 Answers

You may like to use Object.keys() or other Object function, this code may help you

function isEmpty(obj) {
    return !obj || Object.keys(obj).length === 0;
}
like image 189
Arif Khan Avatar answered Sep 28 '22 09:09

Arif Khan