首页 > 解决方案 > .signInWithEmailandPassword 不是函数

问题描述

我正在关注 Firebase 团队的这段视频,将 Firebase Auth 添加到我的应用程序 ( https://www.youtube.com/watch?v=-OKrloDzGpU )

我已将在 Firebase 中为我的项目生成的 Web 应用程序脚本添加到我的应用程序的 html 部分中,并且基本上复制了其他代码来进行登录和注册,如下面的代码所示

但是我收到了这个错误,我不知道它为什么会触发

TypeError: auth.signInWithEmailandPassword 不是函数。(在 'auth.signInWithEmailandPassword(email, pass)' 中,'auth.signInWithEmailandPassword' 未定义)

HTML 代码

<!DOCTYPE html> <meta charset="utf-8" />


<html>
  <head>
    <script src="https://www.gstatic.com/firebasejs/5.7.0/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/5.7.0/firebase-auth.js"></script>

    <link rel="stylesheet" href="css/styles.css" />
  </head>

  <body>

    <div class="container">
      <input id="txtEmail" type="email" required placeholder="Email" />
      <input id="txtPassword" type="password" required placeholder="Password" />
      <button id="btnLogin" class="button-is-link">Login</button>
      <button id="btnSignUp" class="button-is-info">Sign Up</button>
    </div>




    <script src="js/auth.js"></script>
  </body>
</html>

Auth.js

    (function() {
  // Initialize Firebase
  var config = {
    apiKey: 'API KEY',
    authDomain: 'DOMAIN',
    databaseURL: 'DATABASE',
    projectId: 'ID',
    storageBucket: 'BUCKET',
    messagingSenderId: 'ID'
  };
  firebase.initializeApp(config);

  const txtEmail = document.getElementById('txtEmail');
  const txtPassword = document.getElementById('txtPassword');
  const btnLogin = document.getElementById('btnLogin');
  const btnSignUp = document.getElementById('btnSignUp');

  btnLogin.addEventListener('click', e => {
    const email = txtEmail.value;
    const pass = txtPassword.value;
    const auth = firebase.auth();
    const promise = auth.signInWithEmailandPassword(email, pass);
    promise.catch(e => console.log('e.message'));
  });

  btnSignUp.addEventListener('click', e => {
    const email = txtEmail.value;
    const pass = txtPassword.value;
    const auth = firebase.auth();
    const promise = auth.createUserWithEmailandPassword(email, pass);
    promise.catch(e => console.log('e.message'));

    firebase.auth().onAuthStateChange(firebaseUser => {
      if (firebaseUser) {
        console.log('U are logged in');
      } else {
        console.log('Not logged in');
      }
    });
  });
})();

标签: javascriptfirebasefirebase-authentication

解决方案


确切的方法名称是signInWithEmailAndPassword,在 And 处带有一个大写的“A”。

与 相同createUserWithEmailAndPassword


推荐阅读