firebase 这是关于函数未定义的错误

qij5mzcb  于 2023-01-21  发布在  其他
关注(0)|答案(1)|浏览(167)

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

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);
     // ..
   })
 }

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

yb3bgrhw

yb3bgrhw1#

您也没有将auth传入sendPasswordResetEmail方法,这将导致API错误。
奇怪的是,您的调试器/IDE没有发现您丢失了一个参数,这可能是因为您使用的是内联JS。
把JS移到一个可以从HTML文件调用的独立文件中总是很好的,你可以用html script标签来做。

相关问题