我想从表单中获取数据并将信息存储在firebase中,但是数据没有到达我的实时数据库.有人知道为什么吗?



我有一个firebase帐户和一个开放的项目。我复制了数据库的代码片段,并按照他们的读写教程,但有些东西仍然是不对的,我不知道它是什么。

我知道这个问题已经被问过几次了,但是没有什么能帮助我找出问题的所在。

<script src="config.js"></script>
<script type="module">
// Get the modal
var modal = document.getElementById("myModal");
// Get the button that opens the modal
var btn = document.getElementById("registerBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal 
btn.onclick = function () {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function () {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function (event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
function submitRegister() {
alert("This is working");
var discName = document.getElementById('discordName').value;
var userEmail = document.getElementById('userEmail').value;
var positionSelect = document.getElementById('debatePosition');
var userPosition = positionSelect.options[positionSelect.selectedIndex].text;
var debateTypeSelect = document.getElementById('debateType');
var userDebateType = debateTypeSelect.options[debateTypeSelect.selectedIndex].text;
const now = new Date();
now.setMinutes(now.getMinutes() - now.getTimezoneOffset());
var userDT = document.getElementById('userDateTime').value = now.toISOString().slice(0, 16);
if (discName == "" || userEmail == "" || positionSelect == "None" || userDebateType == "None") {
alert("Plese select something for all the")
} else {
var data = {
name: discName,
email: userEmail,
position: userPosition,
type: userDebateType
}
var database = firebase.database();
var ref = database.ref("users");
ref.set(data);
}
}
</script>

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

配置文件只有他们给你的firebase代码:

import firebase from "firebase/app";
import "firebase/database";
const firebaseConfig = {
apiKey: "AIzaSyCXfAcG9Ux2DuX38IHPbuxFok65uira_I8",
authDomain: "debate-app-2305f.firebaseapp.com",
databaseURL: "https://debate-app-2305f-default-rtdb.firebaseio.com",
projectId: "debate-app-2305f",
storageBucket: "debate-app-2305f.appspot.com",
messagingSenderId: "418618483372",
appId: "1:418618483372:web:89740de5372f5756ee3509",
measurementId: "G-06DX6TELJD"
};
firebase.initializeApp(firebaseConfig)

我不打算以后使用相同的firebaseConfig,所以它很好。这只是为了学习。

我听了那么多教程,但是没有一个有用。我只是想用他们给我的discord用户名将信息存储在数据库中。

您正在导入真的旧版本的Firebase。这适用于我的最新sdk:

const app = firebase.initializeApp(firebaseConfig);
var ref = firebase.database().ref("users");
ref.set({
name: "Hello world",
email: "user@acme.com",
position: 42,
type: "open"
});

有关工作示例(针对您的数据库),请参见:https://stackblitz.com/edit/firebase-rtdb-add-data?file=index.js

相关内容

最新更新