0

I'm use this function to check Answer. Value of compareAns should return true/false but it return undefined.

Return undefined

Data in firebase

function compareAns(quiz, ansIndex) {
    let ansRef = firebase
        .database()
        .ref("Questions/" + quiz + "/ans/" + ansIndex);
    ansRef.on("value", (snapshot) => {
        if (snapshot.exists()) {
            return snapshot.val().is_correct;
        } else console.log("No data");
    });
}
4uckd3v
  • 1
  • 2
  • Because you are not returning anything from the function. The only `return` in that snippet is inside of a call back. And even that callback does not always return a value ... But that has nothing to do with the return value for the function itself. – derpirscher Jan 20 '22 at 08:19
  • This is probably very similar to returning a response from a promise/async request, since you are literally awaiting on the `value` event. You'd probably want `compareAns` to return a promise, that resolves with the value from `snapshot` – Terry Jan 20 '22 at 08:19

0 Answers0