Search code examples
javascriptnode.jsexpressrouterrequest-promise

Detect if promise contains data in nodejs express


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
        }
    })