title: how to retrieve firebase data on first click using javascript
body: <p>when I click login button following code executes,</p>
  <pre><code>function login(){
      var email=document.getElementById(&quot;email&quot;).value;
      var password=document.getElementById(&quot;Password&quot;).value;
      var Real_email=&quot;&quot;;
      var Real_password=&quot;&quot;;
          firebase.database().ref('/login').on('value',function(snapshot){
          snapshot.forEach(function (childsnapshot){
              Real_email=childsnapshot.val().email;
              Real_password=childsnapshot.val().password;
              alert(&quot;test&quot;);
              })
          });
      if(email==Real_email &amp;&amp; password==Real_password){
          alert(&quot;correct username and password&quot;);
          }else{
          alert(&quot;Wrong username or password&quot;);    
          }
      }
  </code></pre>
  <p>on the first button click it gives the unexpected output.
  The output order is,</p>
  <ol>
  <li>Wrong username or password.</li>
  <li>test.(after some time...)</li>
  </ol>
  <p>but when I click the login button again (2nd click) it provides the expected output.
  The output order is,</p>
  <ol>
  <li>test.</li>
  <li>correct username and password.</li>
  </ol>
  <p>I tried below code also,</p>
  <pre><code>async function login(){
      var email=document.getElementById(&quot;email&quot;).value;
      var password=document.getElementById(&quot;Password&quot;).value;
      var Real_email=&quot;&quot;;
      var Real_password=&quot;&quot;;
          await firebase.database().ref('/login').on('value',function(snapshot){
          snapshot.forEach(function (childsnapshot){
              Real_email=childsnapshot.val().email;
              Real_password=childsnapshot.val().password;
              alert(&quot;test&quot;);
              })
          });
      if(email==Real_email &amp;&amp; password==Real_password){
          alert(&quot;correct username and password&quot;);
          }else{
          alert(&quot;Wrong username or password&quot;);    
          }
      }
  </code></pre>
  <p>but it returns same result.</p>
  <p>Therefore how to retrieve firebase real time database data on first click using javascript?</p>