javascript - Firebase does multiple calls even with if/else statement JS -
i building app vue , firebase. new firebase , i've problems it. try store names + emails in database. want check first if email in database , if not, run function store name + email. if email stored in database output alert , cancel submit.
so check of email in database going quite well, output alert, , able retrieve data. problem lays when enter email not in database. when enter new email (and name) check database , return false right away call (i dont know why, that's problem guess) , return true, , alert of being there, @ same time. proceed function store data because output of first call (which false).
my js code:
checkform() { let app = this; if (this.name == '' || this.emailadress == '') { alert('you have fill out form'); } else { app.getfirebase(); } }, getfirebase() { let app = this; var ref = firebase.database().ref().child('/aanmeldingen/'); ref.on('value', function(snapshot) { const array = []; snapshot.foreach(function(childsnapshot) { var checkemail = childsnapshot.val().email; array.push(checkemail); }); const res = array.includes(app.emailadress); console.log(array); console.log(res); if(res) { alert('you have entered giveaway'); } else if (res == false) { app.store(); } }); }, store() { this.step_two = false; this.active_two = false; this.active_three = true; this.step_three = true; let app = this; firebase.database().ref('/aanmeldingen/').push({ username: app.name, email: app.emailadress, }); }
screenshot of console (entered jane, not in database)
you should using once() instead of on(). on()
leaves listener attached, when push data in store()
listener fires again.
Comments
Post a Comment