gpt4 book ai didi

javascript - firebase - Reference.set 失败 : First argument contains undefined in property 'users.undefined.profileImageUrl'

转载 作者:行者123 更新时间:2023-12-02 22:28:27 27 4
gpt4 key购买 nike

我是 firebase 和 Javascript 的新手,我尝试了很多方法来通过 user.uid 获取用户数据存储,但我得到的只是数据存储在“未定义”下”。 enter image description here

enter image description here

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);

});

}

最佳答案

我在你的代码中发现了一个东西,createUserWithEmailAndPassword方法返回一个类型为UserCredential的 promise ,一个包含凭据用户结构的对象,因此您在询问属性uid时犯了一个错误 user(返回的 promise )不存在。

它应该看起来像这样:

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
});

关于javascript - firebase - Reference.set 失败 : First argument contains undefined in property 'users.undefined.profileImageUrl' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58990509/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com