Function returns undefined

Hi,

In my off topic project I have this function

    function milestoneCheck(likeCount) {
       let addOne = -1;
       MileStoneList.findOne({}, (err, milestoneList) => {
          if (err) return console.log("Error: ",err);
             for (let i = 0; i < milestoneList.stone.length; i++) {
                let stoneToCheck = milestoneList.stone[i]['stoneReq'];
                   if (stoneToCheck < likeCount) { addOne++; } 
             }
          console.log(addOne);
          return addOne;   
       }); 
    }

The console.log(addOne); in above code outputs the correct addOne number however the function itself returns undefined.

I have tried moving the return, so if wouldn’t be nested in MileStoneList.findOne:

    function milestoneCheck(likeCount) {
       let addOne = -1;
       MileStoneList.findOne({}, (err, milestoneList) => {
          if (err) return console.log("Error: ",err);
             for (let i = 0; i < milestoneList.stone.length; i++) {
                let stoneToCheck = milestoneList.stone[i]['stoneReq'];
                   if (stoneToCheck < likeCount) { addOne++; } 
             }
          console.log(addOne);   
       }); 
      return addOne;  
    }

but this returns addOne as -1.

Do you have any suggestions what I am doing wrong?

Thank you in advance for your help!

I think that whatever you need to do with addOne, it needs to happen inside the callback; the value returned by the callback is, so to speak, ‘lost’, nobody is there to catch it. Alternatively, you can pass a function to the callback, which the callback calls when it is done with the for loop and in which you put the logic that need to be executed with addOne.