繁体   English   中英

数据没有用JS插入firebase数据库

[英]Data not inserting in firebase database with JS

我正在尝试创建一个登录表单,我希望将该信息保存到我的 firebase 数据库中。 我已经编写了代码,但数据没有添加到我的 firebase

这是代码 html 代码

<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>

这是我用来连接 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> 

这是我用来将数据添加到 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
      })
  }

您确定正在触发 function 吗? 如果您的按钮在表单中,那么它可能会触发默认表单提交。 此外, set()方法返回 promise,因此请尝试重构 function,如下所示:

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')
}

如果按钮是前面提到的形式,请尝试像这样设置类型:

<button id="register" type="button" ></button>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM