简体   繁体   中英

How to fix ReferenceError: firebase is not defined when all SDKs are in the HTML file

I am trying to design a website that has a login form which uses Firebase.

I followed the docs ( https://firebase.google.com/docs/web/setup?authuser=0 ) and added all needed JS SDKs for Firebase and also the Firebase-Auth SDK to my HTML file. I also have a JS file for the onclick events and so on in which I want to use methods from the firebase libraries.

<div class="loginContainer">
    <div class="card"></div>
    <div class="card">
        <h1 class="title">Login</h1>
        <form>
            <div class="input-container">
                <input type="text" id="txtEmail" required="required" />
                <label for="email_field">E-Mail</label>
                <div class="bar"></div>
            </div>
            <div class="input-container">
                <input type="password" id="txtPassword" required="required" />
                <label for="password_field">Password</label>
                <div class="bar"></div>
            </div>
            <div class="button-container">
                <button onClick="login();" id="btnLogin"><span>Go</span></button>
            </div>
        </form>
    </div>
</div>

<script src="assets/js/login.js"></script>

<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/firebasejs/6.1.1/firebase-app.js"></script>

<!-- TODO: Add SDKs for Firebase products that you want to use -->
<!-- Insert these scripts at the bottom of the HTML, but before you use any Firebase services -->
<!-- Add Firebase products that you want to use -->
<script src="https://www.gstatic.com/firebasejs/6.2.0/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/6.2.0/firebase-firestore.js"></script>
<script>
    // Your web app's Firebase configuration
    var firebaseConfig = {
        (the config from my Firebase console)
    };
    // Initialize Firebase
    firebase.initializeApp(firebaseConfig);
</script>
const auth = firebase.auth();

I expected the JS file to recognize the auth() method, but all I got was a "Uncaught ReferenceError: firebase is not defined at login.js:2" in the console.

Add the reference to the js file after the Firebase sdk scripts:

//firebase sdk
<script src="https://www.gstatic.com/firebasejs/6.2.0/firebase-auth.js"></script> 
<script src="assets/js/login.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM