我是Firebase和Javascript的新手,我尝试了许多方法来通过user.uid
获取用户数据存储,但是我得到的只是数据存储在“未定义”下。
uRef的屏幕截图?
https://i.stack.imgur.com/Ga19e.png
这是错误的屏幕截图 https://i.stack.imgur.com/ZiY4e.png
Reference.set失败:第一个参数包含属性'users.undefined.profileImageUrl'中的undefined
代码如下:
var displayName;<br>
var userInfo;<br>
var profile_url;<br>
$("#login-btn").click(function(){<br>
userInfo = firebase.auth().currentUser;<br>
if (userInfo) { <br>
firebase.auth().signOut(); <br>
} else { <br>
var email = document.getElementById('login_email').value; <br>
var password = document.getElementById('login_pw').value; <br>
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(){ <br>
var email = document.getElementById('join_email').value;<br>
var password = document.getElementById('join_pw').value;<br>
var displayName = document.getElementById('join_name').value;<br>
var job = $('input[name=job]:checked').val();<br>
firebase.auth().createUserWithEmailAndPassword(email, password).then(function(user) {<br>
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) {<br>
var errorCode = error.code;<br>
var errorMessage = error.message;<br>
alert(error);<br>
});<br>
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 :(得分:0)
错误消息很清楚:
第一个参数包含未定义的属性'users.undefined.profileImageUrl'
您将undefined
作为users.undefined.profileImageUrl
中的值传递,这是不允许的。引发此错误的最可能的位置是:
u_Ref.set({
'email' : email,
'job': job,
'profileImageUrl':profile_url,
'uid' : user.uid,
'username': displayName
});
在这里,最可能的原因是profile_url
尚未初始化,因为您尚未上传图片,
最简单的解决方法是仅在profileImageUrl
具有值的情况下设置profile_url
,您可以通过将其分为两组来轻松实现:
u_Ref.set({
'email' : email,
'job': job,
'uid' : user.uid,
'username': displayName
});
if (profile_url) {
u_Ref.child('profileImageUrl').set(profile_url)
}
我想您还想在图像上传稍后完成时设置profile_url
,所以您可能需要执行以下操作:
snapshot.ref.getDownloadURL().then(function(url) {
console.log('File available at', url);
profile_url = url;
let user = firebase.auth().currentUser;
if (user && user.uid) {
var u_Ref = firebase.database().ref('users/' + user.uid);
u_Ref.child('profileImageUrl').set(profile_url)
}
});