• Jobs
  • About Us
  • Jobs
    • Home
    • Jobs
    • Courses and challenges
  • Businesses
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

84
Views
Data not inserting in firebase database with JS

Im trying to make a login form and i want that info to be save to my firebase database. I have written the code but the data doesn't get added to my firebase

Here is the code html code

<input type="password" id="password"  placeholder="Ur password" required
style="background-color: white; border: none; top: 55%; left: 41.5%; position: absolute; width: 15%; height: 3%;">




<button id="register"  style="position: absolute; left: 48%; color: green; top: 60%; border-radius: 2%; border-color: green;" >Continue</button>

Here is the code i used to connect to firebase

<script src="https://www.gstatic.com/firebasejs/9.1.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.1.1/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.1.1/firebase-database.js"></script> 

and here is the code im using to add the data to firebase:

<script id="main">
        const firebaseConfig = {

            ***The config info is here but i removed it for privacy reasons***

  };


  const app = initializeApp(firebaseConfig);
  var username, password;
  function ready (){
      username = document.getElementById('username').value;
      password = document.getElementById('password').value;
  }


  document.getElementById('register').onclick = function(){
      ready();
      firebase.database().ref('user/'+ username).set({
          user: username,
          pass: password
      })
  }
over 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Are you sure the function is being triggered? If your button is in a form then it might trigger the default form submission. Also the set() method returns promise so try refactoring the function like this:

document.getElementById('register').onclick = async function() {
  // async function                           ^^^^^
  console.log('Register button clicked')
  ready();

  // add await 
  await firebase.database().ref('user/'+ username).set({
    user: username,
    pass: password
  })

  console.log('Date Added To Firebase')
}

If the button is in a form as mentioned earlier, try setting the type like this:

<button id="register" type="button" ></button>
over 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Show me some job opportunities
There's an error!