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)
    })
}
2
  • Does your sQuery return array when it is written correctly to return records? Commented May 7, 2018 at 9:09
  • this is how the result looks when query is written correctly: { id: 50, first_name: 'j', last_name: 'j', email: '[email protected]', mobile_number: 95659739, avatar: 'images/avatar-1525533262516.jpg', active: 1 } Commented May 7, 2018 at 9:15

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.