【问题标题】:Firebase Update email address HTML formFirebase 更新电子邮件地址 HTML 表单
【发布时间】:2017-06-11 19:20:50
【问题描述】:

我正在创建一个 HTML 页面,用户可以在其中更新他们的电子邮件地址。我附加了一个事件监听器,它将使用用户在表单中提供的电子邮件地址更新 Firebase。

下面的代码给出了一个错误:未捕获的类型错误:无法在 HTMLButtonElement 处读取 null 的属性“updateEmail”。

updateEmail 出现在 Firebase 文档中,所以我很困惑。有什么我想念的吗?感谢您的帮助!

Js

"use strict";
(function () {

// get the form elements from HTML
var acctEmail = document.getElementById('acctEmail'),
    acctPassword = document.getElementById('acctPassword'),
    btnUpdate = document.getElementById('btnUpdate'),
    btnDeleteAcct = document.getElementById('btnDeleteAcct'),
    auth = firebase.auth(),
    user = firebase.auth().currentUser, // get the current user
    emailtoval = acctEmail.value, // get the email from acctEmail input field
    email = JSON.stringify(emailtoval); // convert email to json string


// update email
btnUpdate.addEventListener('click', function (e) {
     user.updateEmail(email).then(function() {
        // Update successful.
        }, function(error) {
          console.log(error);
         });
    });


})(); // end Js file wrapper

这是 HTML 表单:

<!DOCTYPE html>
<html> 
<head> 
     <!-- head include -->
    <% include includes/head.ejs %>
</head>

<body id="accountbody">
    <!-- header include -->
    <% include includes/header.ejs %>

    <main>
        <h1> Manage Your Account </h1>

        <!-- begin account details form -->
        <h2>Update your account</h2>
        <div class="updateacctform">
            <input id="acctEmail" type="email" placeholder="Email">

            <input id="acctPassword" type="password" 
placeholder="Password">

            <button id="btnUpdate" class="updateacctbtn">Update
            </button>
        </div>

        <div>
            <h2>Delete your account</h2>
            <button id="btnDeleteAcct" class="deleteacctbtn"> Delete 
Account
            </button>
        </div>

    </main>

    <!-- footer include -->
    <% include includes/footer.ejs %>

    <!-- Js files -->
    <script src="javascripts/account.js"></script>
</body>
</html>

【问题讨论】:

    标签: javascript html firebase firebase-authentication


    【解决方案1】:

    我看不到您在哪里初始化 Firebase 应用实例。您也不需要等待当前用户准备好。您需要调用以下代码:

    // Initialize Firebase app.
    firebase.initializeApp(config);
    // Wait for auth state to be ready.
    firebase.auth().onAuthStateChanged(function(user) {
      if (user) {
        // User is logged in. you can access firebase.auth().currentUser.
        // You can now perform operations on the user.
      } else {
        // No user logged in.
      }
    });
    

    【讨论】:

    • 谢谢,帮了大忙。我让它工作了。我会将其发布在此处作为对您答案的修改。
    • firebase.auth().onAuthStateChanged(function (firebaseUser) { if (firebaseUser) { var newEmail = document.getElementById('acctEmail').value; var user = firebase.auth().currentUser ; user.updateEmail(newEmail).then(function() { // 成功 user.sendEmailVerification().then(function() { // 电子邮件发送 }, function(error) { // 错误 }); }, function( error) { // 错误 }); } });
    猜你喜欢
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    • 2021-04-25
    • 2016-10-04
    相关资源
    最近更新 更多