我是firebase和Javascript的新手,我尝试了许多方法来通过user.uid
获取用户数据存储,但是我得到的只是数据存储在“未定义”下。
var displayName;
var userInfo;
var profile_url;
$("#login-btn").click(function(){
userInfo = firebase.auth().currentUser;
if (userInfo) {
firebase.auth().signOut();
} else {
var email = document.getElementById('login_email').value;
var password = document.getElementById('login_pw').value;
firebase.auth().signInWithEmailAndPassword(email, password).then(function(){
document.getElementById('login_bar').style.display = "none";
document.getElementById('logout_bar').style.display = "inline-block";
document.getElementById('userinfo_bar').style.display = "inline-block";
document.getElementById('secession_bar').style.display = "inline-block";
alert("login success");
location.replace('board.html');
}).catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode === 'auth/wrong-password') {
alert('wrong-password.');
}
else {
alert(errorMessage);
}
console.log(errorMessage);
});
}
});
$("#join-btn").click(function(){
var email = document.getElementById('join_email').value;
var password = document.getElementById('join_pw').value;
var displayName = document.getElementById('join_name').value;
var job = $('input[name=job]:checked').val();
firebase.auth().createUserWithEmailAndPassword(email, password).then(function(user) {
var u_Ref = firebase.database().ref('users/' + user.uid);
userInfo = user;
u_Ref.set({
'email' : email,
'job': job,
'profileImageUrl':profile_url,
'uid' : user.uid,
'username': displayName
});
handleFileSelect();
alert("join success");
}, function(error) {
var errorCode = error.code;
var errorMessage = error.message;
alert(error);
});
if (email.length < 4) {
alert('x');
return;
}
if (password.length < 4) {
alert('x');
return;
}
function initApp() {
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
var displayName = user.displayName;
var email = user.email;
var emailVerified = user.emailVerified;
var photoURL = user.photoURL;
var isAnonymous = user.isAnonymous;
var uid = user.uid;
var providerData = user.providerData;
user.updateProfile ({
displayName : displayName,
}). then (function () {
console.log ( 'success');
}). catch (function (error) {
console.log( 'no');
});
userInfo = user;
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION);
document.getElementById('login_bar').style.display = "none";
document.getElementById('logout_bar').style.display = "inline-block";
document.getElementById('userinfo_bar').style.display = "inline-block";
document.getElementById('secession_bar').style.display = "inline-block";
$("#secession_bar").click(function(){
user.delete().then(function(){
alert('success');
userRef= firebase.database().ref('users/' + user.uid);
userRef.remove();
firebase.storage().ref().child('userImages/' + userInfo.uid).delete();
location.reload();
});
});
$("#logout_bar").click(function(){
firebase.auth().signOut();
location.reload();
});
} else {
document.getElementById('login_bar').style.display = "inline-block";
document.getElementById('logout_bar').style.display = "none";
document.getElementById('userinfo_bar').style.display = "none";
document.getElementById('secession_bar').style.display = "none";
}
});
}
function handleFileSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
var file = evt.target.files[0];
var metadata = {
'contentType': file.type
};
var storageRef = firebase.storage().ref().child('userImages/' +userInfo.uid);
storageRef.put(file, metadata).then(function(snapshot) {
console.log('Uploaded', snapshot.totalBytes, 'bytes.');
console.log('File metadata:', snapshot.metadata);
snapshot.ref.getDownloadURL().then(function(url) {
console.log('File available at', url);
profile_url = url;
});
}).catch(function(error) {
console.error('Upload failed:', error);
});
}
答案 0 :(得分:1)
我在您的代码中发现了一个东西,createUserWithEmailAndPassword
方法返回了一个UserCredential类型的promise,该对象包含一个 credential 和 user的结构,因此您在其中询问了 user 用户的属性 uid 返回的诺言)。
它应该看起来像这样:
firebase.auth().createUserWithEmailAndPassword(email, password).then(function(credential) {
var userInfo = creadential.user;
var u_Ref = firebase.database().ref('users/' + userInfo.uid);
u_Ref.set({
'email' : email,
'job': job,
'profileImageUrl':profile_url,
'uid' : userIfo.uid,
'username': displayName
});