0

I'm trying to start a service and everything is working fine, except whenever I try to console.log the returned data, it just says "undefined"

I'm using a mongodb database to store my info, the function works and has no issues, it's just the return issue.

Any help would be great

--Edit: It's nothing to do with not allowing enough time for the function to complete, I've used setTimeout's and that doesn't help, it still continues to be undefined.

function checkmembership(filter) {
  usermodel.findOne(
    filter,
    (err, data) => {
      if (err) {
        console.log(err);
        client.commands.get('error').execute(client, err, `'checkMembership' method`);
      }
      if (data) {
        console.log('gotdata')
        if (data.MemberShip === true) {
          console.log('true')
          const date = new Date()
          console.log(`${data.EndDate} and ${date.getTime()}`)
          if (data.EndDate > date.getTime()) {
            console.log('returning true?')
            return "true";
          } else if (data.EndDate < date.getTime()) {
            data.MemberShip = false
            data.EndDate = null
            data.UserKey = null
            data.save().catch(err => {
              console.log(err)
              client.commands.get('error').execute(client, err, "'checkMembership' function");
            });
            return "false";
          };
        } else console.log('fasle');
        const words = "false";
        return words;
      } else return "false";
    }
  );
}
Vinnyy
  • 1
  • 1
  • Your checkmembership function has no return statement - it just executes usermodel.findOne(). If your goal is to return whatever usermodel.findOne() returns, then you should write `return usermodel.findOne(...)`. – antun Jun 29 '21 at 17:24
  • 1
    @antun that won't be enough because the return value of `usermodel.findOne(...)` is probably also undefined. It looks to be using a callback – evolutionxbox Jun 29 '21 at 17:25
  • https://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function does this help you? – Tushar Shahi Jun 29 '21 at 18:58
  • @antun there is return statements in the above code snippet, it is just when I'm trying to get the returned data (which is true or false) when I call the function, it says undefined – Vinnyy Jun 30 '21 at 04:11
  • @TusharShahi not from what I read on it – Vinnyy Jun 30 '21 at 04:15
  • 2
    Does this answer your question? [How to return value from an asynchronous callback function?](https://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function) – zero298 Jun 30 '21 at 06:16
  • 1
    @Vinnyy the return statements are inside of a *callback* that's inside your function. The code `(err, data) => {...}` defines a callback function, which is asynchronous. (And in this case, that function is being passed as an argument to usermodel.findOne.) Putting a return statement in there does **not** return from `checkmembership()` function. If you're trying to return *that* value, then see the threads that @Tushar Shahi and @zero298 provided. – antun Jun 30 '21 at 14:43

0 Answers0