Home > OS >  Detect if promise contains data in nodejs express
Detect if promise contains data in nodejs express

Time:09-24

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

})

CodePudding user response:

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