[英]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. 我正在尝试设计一个具有使用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. 我遵循了文档( https://firebase.google.com/docs/web/setup?authuser=0 ),并将Firebase所需的所有JS SDK和Firebase-Auth SDK添加到了我的HTML文件中。 I also have a JS file for the onclick events and so on in which I want to use methods from the firebase libraries.
我还有一个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();
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. 我希望JS文件能够识别auth()方法,但是我得到的只是控制台中的“ Uncaught ReferenceError:firebase未在login.js:2上定义”。
Add the reference to the js file after the Firebase sdk scripts: 在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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.