Detect if promise contains data in nodejs express

Issue

I have a nodejs application that returns a promise. I need help with determining if the promise "result" contains data or if its an empty array. I have tried using Object.keys(result).length === 0 but unfortunately was unsuccessful. Any advice will do. Thanks

router.route('/user/:userCheck/').get((request, response) => {

dboperations.getUser(request.params.userCheck).then(result => {
    if (Object.keys(result).length === 0) {
        //do something;
    } else {
        // do something
    }
})

})

Solution

dboperations.getUser(request.params.userCheck).then(result => {
    if (result.length > 0) {
        //it has data
    } else {
        // no data
    }
})

Answered By – akicsike

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published