0

I made a form and give some validations on submitting and I also want to give validation of if the email that user use in sign up is already exist or not in firebase database ,for which first I have to get the email child from database ,I already done it .The emails are printing in the console and I give variable to that specific one like

<input type="email" name="mail" id="email" placeholder="Email" />

        <p id="emailerr"></p>
ref.on('value', snapshot => {

        snapshot.forEach(function (snapChild) {

            var fireemail = snapChild.child('email').val();
            console.log(fireemail)
        })

    });

This is on submit function


function onSubmit(e) {
var check
    e.preventDefault()

if (email==fireemail){
emailerr.innerHTML='EMAIL ALREADY EXIST'
check=false;
}
if(check==false){
alert("Not submit")}
}else{
writeData(fname, email, pass, my_male, my_female)
        location.reload()
        alert('Form submitted successfully')
}

The variable fireemail is not recognise by the submit function,it gives undefined and instead of giving error it submits the form in the database

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Azhar khan
  • 23
  • 3
  • Data is retrieved from Firebase asynchronously, and your main JavaScript continues to run while the data is being loaded. My guess is that (in addition to what rhl mngl mentioned in their answer) your `if (email==fireemail)` runs before the `var fireemail = snapChild.child('email').val()` gets executed. For this reason all code that needs the data from the database needs to be inside the callback, or be called from there. For a longer explanation and examples, see https://stackoverflow.com/questions/40688268/why-does-firebase-lose-reference-outside-the-once-function/40688890#40688890 – Frank van Puffelen Jan 11 '21 at 15:48
  • In my main code first i get value from firebase and then my submit function is written but it doesn't work, thank you for your answer I will go to the link.. Thankss – Azhar khan Jan 11 '21 at 16:44

1 Answers1

1

Scope of 'fireemail' variable is not global. Declare variable outside of the function.

rhl mngl
  • 53
  • 1
  • 9
  • I already done that but it doesn't work, even I made a function where the var check value is true and false byt ut also didn't work..... – Azhar khan Jan 11 '21 at 14:13