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!