I currently am using data stored in Firebase to check if a user-submitted solution is correct. This involves me calling the function getSolutionAndCompare
when the user presses a “submit” button. However, for some reason, although checkSolution
is returning true to getSolutionAndCompare
, when I call getSolutionAndCompare
in onSubmitPressed
, it evaluates as undefined. Any idea what I could be doing wrong?
Relevant code:
onSubmitPressed: function() {
var output = this.getSolutionAndCompare();
console.log('output ' + output); //this is returning undefined
if (this.getSolutionAndCompare()) {
Alert.alert(
'Correct',
"Woohoo!"
);
}
else {
Alert.alert(
'Incorrect Submission',
'Try again!',
);
}
},
getSolutionAndCompare: function() {
var databaseSolution;
solutionsRef.orderByChild('question_id').equalTo(Number(this.props.questionId)).once('value', (snap) => {
var solution = snap.val();
for (var key in solution) {
databaseSolution = solution[key].solution;
}
return this.checkSolution(databaseSolution); //checkSolution returns true here
});
},
checkSolution: function(databaseSolution) {
if (this.state.submission == databaseSolution) {
return true;
}
else {
return false;
}
},