I'm having a persistent problem when trying to return a value from any of my async functions for my mongoose service.
What's happening is that it doesn't ever want to return the value, just offering an undefined in most cases, leading me to not know what's going on here. I'm pretty new to this sort of thing so someone who can lend a hand would be a god send.
If I log the value in the getBalance function, it works perfectly fine and the balance is shown as expected.
async getBalance(username) {
await users.find({username: username}, function(err, res) {
return(res[0].balance);
});
}
The code below is how I use the function, I await the result (which is always undefined).
async removeCoins(username, amount) {
var coins = await parseInt(this.getBalance(username));
coins -= amount;
await users.updateOne({ username: username }, { balance: coins }, function(err, result) {
if (err) { console.log(err); return false; }
return true;
});
}
As a result I get the following error: CastError: Cast to Number failed for value "NaN" at path "balance"
Any help on this to try and help why I'm not getting a returned value at all would be much appreciated. As explained before If I log the balance inside the get balance function it gets the function perfectly fine.