数据不插入firebase数据库与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
})
}

您确定正在触发该函数吗?如果您的按钮在表单中,那么它可能会触发默认的表单提交。此外,set()方法返回promise,所以尝试像这样重构函数:

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>

相关内容

  • 没有找到相关文章

最新更新