5

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)
    })
}
July333
  • 251
  • 1
  • 6
  • 15
  • Does your `sQuery` return array when it is written correctly to return records? – Ankit Agarwal May 07 '18 at 09:09
  • this is how the result looks when query is written correctly: `{ id: 50, first_name: 'j', last_name: 'j', email: 'j@gmail.com', mobile_number: 95659739, avatar: 'images/avatar-1525533262516.jpg', active: 1 }` – July333 May 07 '18 at 09:15

1 Answers1

24

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;
}
Arif Khan
  • 5,039
  • 2
  • 16
  • 27