javascript - Firebase 更新电子邮件地址 HTML 表单

标签 javascript html firebase firebase-authentication

我正在创建一个 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>

最佳答案

我没有看到您在哪里初始化 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.
  }
});

关于javascript - Firebase 更新电子邮件地址 HTML 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44487807/

相关文章:

Apache 上的 JavaScript "AH01215: (8)Exec format error" "End of script output before headers"

javascript - Bootstrap Modal 改变链接

Firebase 验证电子邮件模板编辑

ios - 查询限制(到最后 :1) firebase Chat Mesenger Swift

javascript - parent 高度80%的 child (高度不详)

javascript - 单击内容区域外部关闭 GLightbox 画廊

javascript - 选择嵌套的 iframe - selenium/javascript/node-js

php - 如何清除此错误

.net - c#编码转换?

javascript - 火力地堡 : Anonymous authentication - How to set identifier?