Firebase 帐户创建尝试使用变量设置附加信息,然后获取该信息

Firebase account creation trying to set additional info using variables and then getting that information

所以,在一份文件 (register.html) 上,我有这个代码:

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') {
                      weakPass.style="color:red;";
                    }

                            else {
                        unexpected.innerHTML=errorMessage;
                    }
                    console.log(error);

//Handle Account Status
    firebase.auth().onAuthStateChanged(user => {

    if(user) {
            var user = firebase.auth().currentUser;
            var firstname = txtFirst.value;
            var lastname = txtLast.value;
            var School = dropdown.value;

            user.updateProfile({
                firstName: firstname,
                lastName: lastname,
                school: School
            }).then(function() {
                alert("User: " + user + " Firstname: " + user.firstName + " Lastname: " + user.lastName + " School: " + user.school);
                redirect("home.html");
            });


            //After successful login, user will be redirected to home.html
    }

    });

虽然用户确实使用连接到 firebase 网站的电子邮件和密码注册,并成功将网站重定向到 home.html,但我认为它实际上并没有添加数据给用户 "profile".

我在 home.html 上有这段代码试图提取数据:

//Handle Account Status
  firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
      // User is signed in.
      var firstname = user.firstName;
      var lastname = user.lastName;
      var school = user.school;

      alert("User: " + user + " Firstname: " + firstname + " Lastname: " + lastname + " School: " + school);
    } else {
      // User is signed out.
    }

  });

但是,这不起作用。 user的值为true,但是alert时的alertreturnsundefined是叫。警报准确地显示了这一点: "User: [object Object] Firstname: undefined Lastname: undefined School undefined"

我做错了什么?有没有 different/better 使用 firebase 存储用户数据的方法? 我为用户存储的唯一数据是他们的名字、姓氏和学校,以及明显的电子邮件和密码,因为这确实有效。

老实说,我不知道我设置数据或提取数据的方式是否有问题,或者如何测试。

根据 guides

You can update a user's basic profile information—the user's display name and profile photo URL—with the updateProfile method.

因此,您可以使用 updateProfile() 更改的唯一值是 displayNamephotoUrl。无法向用户添加任意​​配置文件字段。相反,您需要在数据库中存储有关用户的额外信息,例如 Cloud Firestore. If you'd like more info on Cloud Firestore,此 Codelab 是一个很好的起点。