我正在尝试从Firebase数据库获取Web应用程序中所有用户的名称。这是我的数据库结构:
在“名称”表中,我试图获取所有名称(A
,B
...)。这是我一直在尝试的:
我的html:
<input type="email" placeholder="Email..." id="email_field" />
<input type="password" placeholder="Password..." id="password_field" />
<button onclick="login()">Login to Account</button>
<div>
<table>
<thead>
<tr>
<td>Name</td>
</tr>
</thead>
<tbody id="table_body">
</tbody>
</table>
</div>
<script src="https://www.gstatic.com/firebasejs/5.4.1/firebase.js"></script>
<script>
var config = {
apiKey: "AIzaSyBYa4OCyyWNzTpWYHA5KeQ",
authDomain: "example.firebaseapp.com",
databaseURL: "https://example.firebaseio.com",
projectId: "example",
storageBucket: "example.appspot.com",
messagingSenderId: "77687691833822",
};
firebase.initializeApp(config);
</script>
<script src="log.js"></script>
log.js
是:
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
var user = firebase.auth().currentUser;
if (user != null) {
var email_id = user.email;
var rootRef = firebase.database().ref().child("Users");
rootRef.on("child_added", snap => {
var name = snap.child("Name").val();
$("#table_body").append("<tr><td>" + name + "</td></tr>");
});
}
} else {
}
});
function login() {
firebase.auth().signInWithEmailAndPassword(userEmail, userPass).catch(function(error) {
//...
});
}
但是它没有显示数据库中的任何数据。我在某处犯了一些错误,但不确定是什么。预先感谢您的建议。