【问题标题】:I get an Error when I try to use Firebase Auth尝试使用 Firebase 身份验证时出现错误
【发布时间】:2021-05-24 00:14:22
【问题描述】:

我第一次构建包含 firebase (firebase auth) 的网站。 在浏览器中运行 HTML 代码时出现错误。

未捕获的类型错误:firebase.auth 不是函数 在 HTMLButtonElement。 (app.js:24)

我也没有找到任何关于这个问题的文章。 我已经重新安装了节点和 firebase 工具。 有大佬解答一下,我真的不知道怎么办。。。 这是我的 Index.html 文件和我的 App.js 文件 索引.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Log In Page</title>
    <link rel="stylesheet" href="style.css">
    <script src="https://www.gstatic.com/firebasejs/8.2.9/firebase-app.js"></script>
</head>
<body>
    <div class="Main-Div">
        <h3 class="Headline">
            Do you have an Account?
        </h3>
        <input type="text" placeholder="Your Email..." class="AccountInput" type="email" id="txtemail">
    </br>
        <input type="text" placeholder="Your Passcode..." class="AccountInput" type="Password" id="txtpassword">
    </br>
        <button class="LogInButton" id="1Button">Log In</button>
        <button class="SignUpButton" id="2Button">Sign Up now!</button>
        <button class="LogOutButton" id="3Button">Log Out</button>
    </div>
    <script src="app.js"></script>
</body>
</html>

这里是我的另一个文件,App.js:(我已经审查了有关我的 firebase-database 的数据)

(function(){
const firebaseConfig = {
    apiKey: "Censored",
    authDomain: "Censored",
    projectId: "Censored",
    storageBucket: "Censored",
    messagingSenderId: "Censored",
    appId: "Censored"
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);

// Get Elements
  const txtEmail = document.getElementById('txtemail');
  const txtPassword = document.getElementById('txtpassword');
  const btnLogIn = document.getElementById('1Button');
  const btnSignUp = document.getElementById('2Button');
  const btnLogOut = document.getElementById('3Button');

  btnLogIn.addEventListener('click', e => {
    const email = txtEmail.value;
    const password = txtPassword.value;
    const auth = firebase.auth();

    const promise = auth.signInWithEmailAndPassword(email, pass);
    promise.catch(e => console.log(e.message));
  });

}());

感谢您的关注 你好 最大

【问题讨论】:

    标签: javascript html firebase firebase-authentication


    【解决方案1】:

    index.html 中,您只导入了核心firebase 库firebase-app.js,以便在您的网络应用程序中使用firebase 服务(firestore/storage 等),您需要从CDN 导入相应的服务。

    在您的情况下,您需要在您的&lt;head&gt;&lt;/head&gt; 中添加firebase-auth.js

    这是链接

    <script src="https://www.gstatic.com/firebasejs/8.2.8/firebase-auth.js"></script>
    

    在这个page你可以找到所有的服务CDN

    您还需要转到项目的 firebase 仪表板并启用密码身份验证,如documentation中所述

    【讨论】:

    • 抱歉耽搁了,这个错误很明显,在代码编辑器中转到第 25 行并查找“pass”,您会发现您将变量命名为“password”而不是“pass”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 2019-12-21
    • 2021-01-23
    • 1970-01-01
    • 2019-04-18
    相关资源
    最近更新 更多