Javascript Firebase在创建帐户后返回用户对象
我试图在Firebase中创建一个用户,然后在web服务器上的数据库中创建一个用户配置文件。我已经实现了下面的代码,它很好地创建了用户。但是,我不知道如何接收用户id(我需要一个唯一的id)来创建数据库结构。调用createUserWithEmailAndPassword时,是否有方法返回用户对象 我已尝试实现Javascript Firebase在创建帐户后返回用户对象,javascript,firebase,firebase-realtime-database,firebase-authentication,Javascript,Firebase,Firebase Realtime Database,Firebase Authentication,我试图在Firebase中创建一个用户,然后在web服务器上的数据库中创建一个用户配置文件。我已经实现了下面的代码,它很好地创建了用户。但是,我不知道如何接收用户id(我需要一个唯一的id)来创建数据库结构。调用createUserWithEmailAndPassword时,是否有方法返回用户对象 我已尝试实现firebase.auth().onAuthStateChanged函数,但随后收到超时错误 如果你还没有收集,这是一个网络应用程序 <script> function cre
firebase.auth().onAuthStateChanged
函数,但随后收到超时错误
如果你还没有收集,这是一个网络应用程序
<script>
function createUser() {
var Result = "true";
var textUser = document.getElementById('userName').value;
var textPassword = document.getElementById('userPassword').value;
var textAccountID = document.getElementById('accountRef').value;
var textDateCreated = document.getElementById('dateCreated').value;
var textDisplayName = document.getElementById('displayName').value;
var UID;
firebase.auth().createUserWithEmailAndPassword(textUser, textPassword).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
return Result = "false";
// ...
});
writeUserData(UID, textDisplayName, textAccountID, textDateCreated);
return Result;
}
function writeUserData(userId, displayName, accountID, dateCreated) {
firebase.database().ref('User/' + userId).set({
userId:{
AccountID: accountID,
Created: dateCreated,
Name: displayName}
});
}
</script>
函数createUser(){
var Result=“true”;
var textUser=document.getElementById('userName')。值;
var textPassword=document.getElementById('userPassword')。值;
var textAccountID=document.getElementById('accountRef')。值;
var textDateCreated=document.getElementById('dateCreated').value;
var textDisplayName=document.getElementById('displayName')。值;
变量UID;
firebase.auth().createUserWithEmailAndPassword(textUser,textPassword).catch(函数(错误){
//在这里处理错误。
var errorCode=error.code;
var errorMessage=error.message;
返回结果=“false”;
// ...
});
writeUserData(UID、textDisplayName、textAccountID、textDateCreated);
返回结果;
}
函数writeUserData(userId、displayName、accountID、dateCreated){
firebase.database().ref('User/'+userId).set({
用户标识:{
AccountID:AccountID,
创建日期:dateCreated,
名称:displayName}
});
}
要在客户端获取用户id,您应该执行以下操作:
firebase.auth().createUserWithEmailAndPassword(textUser, textPassword)
.then(function(user){
console.log('uid',user.uid)
//Here if you want you can sign in the user
}).catch(function(error) {
//Handle error
});
正如这里所描述的:
返回
非空firebase.Promise包含非空firebase.User函数
createUserWithEmailAndPassword()
返回一个所谓的Promise,该函数有方法catch()
和then()
。您已经在使用catch()
来处理问题。要处理“非问题”,您需要使用then()
:
有关和,请参阅参考文档。看起来firebase对此方法做了一些更改。可接受的解决方案需要一些修复:
firebase.auth().createUserWithEmailAndPassword(textUser, textPassword)
.then(function(data){
console.log('uid',data.user.uid)
//Here if you want you can sign in the user
}).catch(function(error) {
//Handle error
});
现在您可以通过data.user访问用户
希望这有帮助:)从您的文档链接:“成功创建用户帐户后,此用户也将登录到您的应用程序。”您是对的…旧版本中没有发现更改。。。谢谢你,这种行为已经完全改变了。我一直被它咬着。我收到一个身份验证/网络请求失败错误,执行上述操作。调试建议不调用.then承诺返回的承诺是否更改?我无法访问user.uid,相反,我必须执行类似data.user.uid.im的操作,因为执行上述操作时收到身份验证/网络请求失败错误。调试建议不调用.then承诺根据新的更改,这是正确的。谢谢,救了我一天。
firebase.auth().createUserWithEmailAndPassword(textUser, textPassword)
.then(function(data){
console.log('uid',data.user.uid)
//Here if you want you can sign in the user
}).catch(function(error) {
//Handle error
});
firebase.auth().createUserWithEmailAndPassword(email.value, password.value).then(function (user) {
//var user = firebase.auth().currentUser;
alert('Cadastro feito com sucesso!', "success");
console.log("New user Id: "+ user.uid);
}).catch(function (error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// [START_EXCLUDE]
if (errorCode == 'auth/weak-password') {
alert('A senha é muito fraca.', "warning");
//alert('A senha é muito fraca.');
} else if (errorMessage == "The email address is already in use by another account.") {
errorMsg = "Esse email já está sendo usado por outra conta";
alert(errorMsg, "danger");
//alert(errorMessage);
}else{
alert(errorMessage);
}
// [END_EXCLUDE]
});