1

I am working with monk, and when I look for a user in the database using a function, I want their to return a boolean depending on if its found or not. Here is the code:

Function:

function userExist() {
users
    .find()
    .then(result => {
        console.log(result)
        if(result.length != 0) {
            return true;
        } else {
            return false;
        }
    })

}

calling the function(here is the error):

userExist().then(received => {
        console.log(received);
})
  • See also [Why is value undefined at .then() chained to Promise?](https://stackoverflow.com/questions/44439596/why-is-value-undefined-at-then-chained-to-promise/44439597#44439597) – guest271314 Feb 01 '19 at 00:39

1 Answers1

1

You should return your promise inside your userExist function. Otherwise, you have just a function and not a promise to call then().

function userExist() {
return users
  .find()
  .then(result => {
      console.log(result)
      if(result.length != 0) {
          return true;
      } else {
          return false;
    }
})
LuisMendes535
  • 526
  • 7
  • 18