【问题标题】:This is a error about function not defined这是关于函数未定义的错误
【发布时间】:2023-01-20 18:38:01
【问题描述】:

在我的 html 文件中,我已经为另一个 js 文件 sigin.js 中定义的 forgot password() 函数定义了 onclick 事件,但在实时服务器上它给出了错误:

VM19 signin.html:20 Uncaught ReferenceError: forgotPass is not defined
    at HTMLAnchorElement.onclick (VM19 signin.html:20:67)

这是我的 html 代码:

Signin





    <img src="users.png">
    <h2>Sign in</h2>
    

        <input type="text" id="username" class="input-box" placeholder="username"><br>
        <input type="email" id= "email" class="input-box" placeholder="email id"><br>
        <input type="password" id="password" class="input-box" placeholder="passsword"><br>
        <a href="#" class="forgetpass" onclick="forgotPass()">forget password?</a>
    
        <button type="submit" id="signIn" class="signin-btn">Sign in</button>
    
        <hr>
        <p class="or" style="color: rgb(255, 0, 64);">Or sign in with</p>
         
        <a href="#"><i class="fab fa-facebook-f"></i></a>
        <a href="#"><i class="fab fa-instagram"></i></a>
        <a href="#"><i class="fab fa-twitter"></i></a>
        <a href="#"><i class="fab fa-linkedin-in"></i></a><br>
       
        <p style="color: rgb(255, 0, 64);">Don't have an account? <a href="signup.html" style="color: rgb(255, 0, 64);">Create account</a></p>
           
        
    </form>
</div>

//This is my js file:

import { initializeApp } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-app.js";
import { getDatabase,ref,update } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-database.js";
import { getAuth, signInWithEmailAndPassword,sendPasswordResetEmail } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-auth.js";

// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

// Your web app's Firebase configuration
const firebaseConfig = {
  apiKey: "AIzaSyDmNExTV5gaczHbcsrTXjNvab7vmug0rIw",
  authDomain: "authentication-app-2de5b.firebaseapp.com",
  databaseURL: "https://authentication-app-2de5b-default-rtdb.firebaseio.com",
  projectId: "authentication-app-2de5b",
  storageBucket: "authentication-app-2de5b.appspot.com",
  messagingSenderId: "932491620237",
  appId: "1:932491620237:web:5a2f2038c025dd3a8997c4"
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const database = getDatabase(app);

const auth = getAuth();

var signIn = document.getElementById("signin-form");
signIn.addEventListener('submit',(e)=>{
    e.preventDefault();
   var email = document.getElementById("email").value;
   var password = document.getElementById("password").value;
   var username = document.getElementById("username").value ;
   signInWithEmailAndPassword(auth, email, password)
  .then((userCredential) => {
    // Signed in 
    const user = userCredential.user;
    const date = new Date();
    update(ref(database,'users/'+ user.uid),{
      last_login : date
   })
   alert("Sign in successfully!!");
   document.getElementById("signin-form").reset();
    // ...
  })
  .catch((error) => {
    const errorCode = error.code;
    const errorMessage = error.message;
    alert(errorMessage);
  });

})
function forgotPass(){
  const email = document.getElementById("email").value;
 sendPasswordResetEmail(email)
   .then(() => {
     // Password reset email sent!
     // ..
     alert("Password reset link sent to your email successfully!!");
   })
   .catch((error) => {
     const errorCode = error.code;
     const errorMessage = error.message;
     alert(errorMessage);
     // ..
   })
 }

我已经尝试了很多,但我没有找到解决这个问题的方法。请尝试找出问题所在,以便我继续:)

【问题讨论】:

  • 请混淆您的 firebase 配置详细信息。

标签: javascript html css firebase web-deployment-project


【解决方案1】:

您已将点击事件分配给忘记密码按钮,该按钮会触发名为 forgotPass 的函数。

您尚未在 JavaScript 文件中定义 forgotPass,因此当它尝试将事件侦听器分配给不存在的函数时,它会抛出该错误。

要修复,请删除点击事件。

 <a href="#" class="forgetpass" >forget password?</a>

我建议您在尝试使用 Firebase 等数据库之前,先充分掌握 JavaScript 和 HTML。网上有很多很好的免费指南,可以帮助您上路。

【讨论】:

    猜你喜欢
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2015-02-24
    • 1970-01-01
    • 2019-10-03
    • 2013-06-19
    • 2013-06-21
    相关资源
    最近更新 更多