Michael Hoefler
Michael Hoefler

Reputation: 93

Properly break a firebase forEach loop

So I have a function in my react native app that needs to check a code entered by the user and compare it to one in the firebase-realtime-database. Currently, I am using a forEach loop to cycle through the code in the db, and comparing them to the entered code. The problem is, a return statement seems to have no effect on this code segment, and it always runs all the way through. I am a total beginner to this, so if there is a better way to do it, I am completely open minded. Here is the problematic code :

function checkCode(text) {
   var code = text;
   codesRef.once('value', function(db_snapshot) {
      db_snapshot.forEach(function(code_snapshot) {
      if (code == code_snapshot.val().value) {
         console.log("Authentication Successful!");
           // break; // throws error
           return; // Does not seem to stop the code segment
      }
   })
   console.log("Authentication Failed!"); // This still runs, even on success...
   //AlertIOS.alert("We're Sorry...", "The code you entered was not found in the database! Please contact Mr. Gibson for further assistance.")
   });
}

The code for my AccessForm.js is below, and I am open to any suggestions, even if it isn't related to the forEach issue.

DropBox : AccessForm

Upvotes: 1

Views: 1063

Answers (2)

thatGuy
thatGuy

Reputation: 122

well... I found this useful Short circuit Array.forEach like calling break

so you would have

function checkCode(text) {
    try {
        var code = text;
        codesRef.once('value', function(db_snapshot) {
            db_snapshot.forEach(function(code_snapshot) {
                if (code == code_snapshot.val().value) {
                    console.log("Authentication Successful!");
                    // break; // throws error
                    //return; // Does not seem to stop the code segment
                    throw BreakException; //<-- use this guy here
                }
            })
            console.log("Authentication Failed!"); // This still runs, even on success...
            //AlertIOS.alert("We're Sorry...", "The code you entered was not found in the database! Please contact Mr. Gibson for further assistance.")
        });
    } catch (e) {
        if (e !== BreakException) throw e;
    }

    //continue code
}

NB. Im pretty new to javascript but it works for me.

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

Once you start a loop with Firebase's DataSnapshot.forEach() you can't abort it. This means that you must capture the state of your check in a variable, and then use that after the loop completes to determine what to print.

So something like:

codesRef.once('value', function(db_snapshot) {
  let isUserFound = false
  db_snapshot.forEach(function(code_snapshot) {
    if (code == code_snapshot.val().value) {
      isUserFound = true
    }
  })
  console.log("Authentication " + isUserFound ? "Successful!" : "Failed!");
});

In case you're looking to return a value from checkCode (which is a common next step), you might want to already read: JavaScript - Firebase value to global variable

Upvotes: 2

Related Questions