gpt4 book ai didi

javascript - 为什么在 createUserWithEmailAndPassword 之后获取 uid 时 Firebase 返回未定义

转载 作者:行者123 更新时间:2023-12-01 01:28:46 25 4
gpt4 key购买 nike

我正在尝试将新注册的用户及其新创建的用户 UID 保存到数据库中。然而它返回未定义,我不知道为什么。这就是我所拥有的

function fb_signup(){
firebase.auth().createUserWithEmailAndPassword(email, password)
.then(function success(userData){
var uid = userData.uid;
var displayName = userData.displayName;
var email = userData.email;
var emailVerified = userData.emailVerified;
var photoURL = userData.photoURL;
var isAnonymous = userData.isAnonymous;
var providerData = userData.providerData;
$('#fbFormMessage').css({'display':'block','background-color':'#c9e9cc','color':'#347f3b'});
document.getElementById("fbFormMessage").innerHTML = 'Welcome Your registration was successful.';
console.log(uid + ' | ' + email + ' | ' + uName )
saveNewUser(uid, email, uName);
return false;
})
.catch(function failure(error) {
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorCode + " " + errorMessage);
$('#fbFormMessage').css({'display':'block','background-color':'#FFEBEE','color':'#EF5350'});
document.getElementById("fbFormMessage").innerHTML = 'The email address is already in use by another account';
});
};




function saveNewUser(a,b,c){
console.log('4')
var rootRef = firebase.database().ref();
var storesRef = rootRef.child('/users/');
var newStoreRef = storesRef.push();
newStoreRef.set({
uid: a,
userName: c,
email: b,
});
};

最佳答案

因为 createUserWithEmailAndPassword() 方法返回 UserCredential (而不是用户),详见文档:https://firebase.google.com/docs/reference/js/firebase.auth.Auth#createUserWithEmailAndPassword

所以你应该这样做:

function fb_signup(){
firebase.auth().createUserWithEmailAndPassword(email, password)
.then(function success(userCredential){
var userData = userCredential.user;
var uid = userData.uid;
....

关于javascript - 为什么在 createUserWithEmailAndPassword 之后获取 uid 时 Firebase 返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53501698/

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