【发布时间】:2018-07-09 04:56:56
【问题描述】:
我是 Firebase 和 JS 的新手。我正在尝试在存储在 Firebase 数据库中的网页上显示一些用户信息。
Data format is something as this image
这是我在一些教程的基础上编写的 Javascript 代码。我无法将数据(电子邮件)从 firebase 数据库检索到网页。
(function() {
// Initialize Firebase
var config = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: ""
};
firebase.initializeApp(config);
//Get elements
const txtEmail = document.getElementById('txtEmail');
const txtPassword = document.getElementById('txtPassword');
const btnLogin = document.getElementById('btnLogin');
const btnSignup = document.getElementById('btnSignup');
const btnLogout = document.getElementById('btnLogout');
//Add login event
btnLogin.addEventListener('click', e => {
//Get email and password
const email = txtEmail.value;
const pass = txtPassword.value;
const auth = firebase.auth();
//Sign in event
const promise = auth.signInWithEmailAndPassword(email,pass);
promise.catch(e => console.log(e.message));
});
btnSignup.addEventListener('click', e => {
const email = txtEmail.value;
const pass = txtPassword.value;
const auth = firebase.auth();
//Sign in
const promise = auth.createUserWithEmailAndPassword(email,pass).then(newUser => {
firebase.database().ref('userProfile').child(newUser.uid).set({email: email });
var userProfileRef = database.ref('userProfile');
userProfileRef.on('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childData = childSnapshot.val();
});
});
});
promise.catch(e => console.log(e.message));
});
btnLogout.addEventListener('click', e => {
firebase.auth().signOut();
});
firebase.auth().onAuthStateChanged(firebaseUser => {
if (firebaseUser) {
var smail = "abcdefgh@gmail.com";
document.cookie = smail;
window.location = 'sample.html';
btnLogout.classList.remove('hide');
} else {
console.log('not logged in');
btnLogout.classList.add('hide');
}
});
}());
【问题讨论】:
-
图片在哪里
-
现在我编辑了当你点击上面的链接时[数据格式是这个图像的东西]图像会弹出。
-
好的,现在你需要用电子邮件做什么?
-
我想将该电子邮件检索到我的网页。
-
用户注册时?还是其他时候?
标签: javascript html firebase firebase-realtime-database firebase-authentication