EmailAndPassword的同时添加displayName

EmailAndPassword的同时添加displayName

本文介绍了在使用createUserWithEmailAndPassword的同时添加displayName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将显示名称添加到刚创建的用户。但是,当我创建UserWithEmailAndPassword它是我的currentUser是空的。有什么不对的?

  var name = document.getElementById(name).value; 
var username = document.getElementById(username).value;
var email = document.getElementById('email')。value;
var password = document.getElementById('password')。value;
//用电子邮件登录并通过。
// [START createwithemail]
firebase.auth()。createUserWithEmailAndPassword(email,password).catch(function(error){
// Handle Errors here。
var errorCode = error.code;
var errorMessage = error.message;
// [START_EXCLUDE]
if(errorCode =='auth / weak-password'){
alert(' ');
} else {
console.error(error);
}
// [END_EXCLUDE]
});
// [END createwithemail]
var user = firebase.auth()。currentUser;
user.updateProfile({
displayName:username
})。(function(){
//更新成功
},function(error){
//发生错误
});
document.getElementById(submitButton)。innerHTML =正在加载...;
$(#submitButton)。css(background-color,#efefef);
$(#submitButton)。css(cursor,init);
$(#submitButton)。css(color,#aaa);
registerUserToDB(username,name,email);
console.log(shouldhave worked);


解决方案

createUserWithEmailAndPassword 与Firebase中几乎所有其他功能类似,都是异步调用。你创建用户(很可能是成功的),但是在你尝试抓取 currentUser 之后立即创建用户。在几乎所有情况下,您都将尝试在Firebase完成用户创建之前获取 currentUser 。



回调:

$ $ p $ firebase.auth()。createUserWithEmailAndPassword(email,password).then(function(user){
// [END createwithemail]
// callSomeFunction();可选
// var user = firebase.auth()。currentUser;
user.updateProfile({
displayName:username
})。then(function(){
//更新成功
},函数(error){
//发生错误
});
},函数(错误){
//处理错误
var errorCode = error.code;
var errorMessage = error.message;
// [ START_EXCLUDE]
if(errorCode =='auth / weak-password'){
alert('The password is too weak。');
} else {
console.error (error);
}
// [END_EXCLUDE]
});

.then 函数(错误)将在错误时被调用。你想在用户创建成功后设置你的用户。



有些人不喜欢嵌套的回调,所以你可以创建一个获取当前用户的函数,并调用功能成功。



文件:




I want to add the display name to the user that was just created. But when I createUserWithEmailAndPassword it sais my currentUser is null. Anything wrong?

var name = document.getElementById("name").value;
var username = document.getElementById("username").value;
var email = document.getElementById('email').value;
var password = document.getElementById('password').value;
  // Sign in with email and pass.
  // [START createwithemail]
  firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
    // [START_EXCLUDE]
    if (errorCode == 'auth/weak-password') {
      alert('The password is too weak.');
    } else {
      console.error(error);
    }
    // [END_EXCLUDE]
  });
  // [END createwithemail]
  var user = firebase.auth().currentUser;
  user.updateProfile({
      displayName: username
    }).then(function() {
      // Update successful.
    }, function(error) {
      // An error happened.
  });
document.getElementById("submitButton").innerHTML = "Loading...";
$("#submitButton").css("background-color", "#efefef");
$("#submitButton").css("cursor", "init");
$("#submitButton").css("color", "#aaa");
registerUserToDB(username, name, email);
console.log("shouldhave worked");
解决方案

createUserWithEmailAndPassword is an asynchronous call like almost every other function in Firebase. You create the user (most likely successfully) but then immediately after you try and grab the currentUser. In almost every case you will be attempting to get the currentUser before Firebase has finished creating your user.

Rewrite inside callback:

firebase.auth().createUserWithEmailAndPassword(email, password).then(function(user) {
    // [END createwithemail]
    // callSomeFunction(); Optional
    // var user = firebase.auth().currentUser;
    user.updateProfile({
        displayName: username
    }).then(function() {
        // Update successful.
    }, function(error) {
        // An error happened.
    });
}, function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
    // [START_EXCLUDE]
    if (errorCode == 'auth/weak-password') {
        alert('The password is too weak.');
    } else {
        console.error(error);
    }
    // [END_EXCLUDE]
});

.then will be called upon success and function(error) will be called upon error. You want to set your user after user creation was successful.

Some people don't like the nested callbacks so you could create a function that gets the current user and call the function upon success.

Docs:

Firebase Promises

Async, callbacks

这篇关于在使用createUserWithEmailAndPassword的同时添加displayName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 21:28