I'm pretty new to async stuff, and I'm rather stuck on this one.
I'm trying to get the result of a mongo query via mongoose, compare it to another value, and return the comparison result from the run() function.
async run() {
const thisHash = await this.getHashOfElement(url, '.edit-text');
var latestHash;
await Invitation.findOne().sort('-updated').exec(async (e, record) => {
if(record != undefined) {
const latestInvitation = record;
latestHash = latestInvitation.hash;
console.log("latestHash inside");
console.log(latestHash);
} else{
throw e;
}
});
console.log('latestHash outside:');
console.log(latestHash);
return latestHash === thisHash;
}
The run() function always returns false, since it thinks latestHash is undefined when the comparison is made.
I thought it would wait until the findOne() is complete, since I put await in front of it, and then perform the comparison. But the outside console log appears before the inside, and it's undefined.
What do I need to do?
Thanks!