如何修复ReferenceError:当所有SDK都位于HTML文件中时,未定义firebase

时间:2019-06-14 23:28:12

标签: javascript html firebase firebase-authentication

我正在尝试设计一个具有使用Firebase的登录表单的网站。

我关注了文档(https://firebase.google.com/docs/web/setup?authuser=0),并将所有Firebase所需的JS SDK和Firebase-Auth SDK添加到了我的HTML文件中。我还有一个onclick事件的JS文件,等等,我想在其中使用firebase库中的方法。

<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();

我希望JS文件能够识别auth()方法,但我得到的只是控制台中的“ Uncaught ReferenceError:在baselog.js:2上未定义firebase”。

1 个答案:

答案 0 :(得分:2)

在Firebase sdk脚本之后添加对js文件的引用:

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