for (var x = 0; x < emails.length; x++){
var email = emails[x]["email"];
client.single.check(email, true, true).then(
result => {
if (result.getNumericResult() == 0){
console.log("valid email: "+email);
var verified = 1;
admin.database().ref("/users/"+userID+"/emails/"+x+"/verified").set(verified, function(error){
if (error) {
console.log("User ("+userID+") email state could not be saved" + error);
}
console.log("Saved");
});
}
},
err => console.log('ERROR: ' + err.message)
);
}
Above is my code.
I have a database entry "emails", which has multiple email addresses, broken out like: (0)-> email: foo@bar.com, (1)-> email: foo1@bar1.com, etc.
What I am trying to do is loop through each email in the emails entry and run a check on that email to validate it. However, I noticed that it will loop to the next item before the validation module (client.single.check) has finished, and as a result will push the wrong "x" (item) in my database path. If it's on the 0th item, it will push to x=1 in the database path where I have "x".
How can I make it so that for each item (email address) in the for loop, it waits to do the email check and also push the result to my database before moving on to the next item in the for loop?
So stuck on this one. Sorry for the newbie question!