我正在尝试创建Google身份验证,但我将var provider = new firebase.auth.GoogleAuthProvider();
复制到我的JavaScript中,它一直告诉我Uncaught ReferenceError: firebase is not defined
。
这是我的代码:
var provider = new firebase.auth.GoogleAuthProvider();
function signin(){
firebase.auth().signInWithPopup(provider).then(function(result) {
var token = result.credential.accessToken;
var user = result.user;
}).catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
var email = error.email;
var credential = error.credential;
});
}
答案 0 :(得分:4)
You are getting this error because firebase
variable has not been instantiated and initialized on the page on which you are running the code. To do so you need to -
Include the relevant scripts-
<script src="https://www.gstatic.com/firebasejs/4.1.3/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.1.3/firebase-auth.js"></script>
firebase.js
contains the variable firebase
representing your firebase app. firebase-auth.js
contains the firebase.auth
library which your code is using.
Initialize your app using -
var config = {
apiKey: "<API_KEY>",
authDomain: "<PROJECT_ID>.firebaseapp.com",
databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
storageBucket: "<BUCKET>.appspot.com",
messagingSenderId: "<SENDER_ID>",
};
firebase.initializeApp(config);
To learn more refer - https://firebase.google.com/docs/web/setup
答案 1 :(得分:0)
Firebase的一个常见陷阱会导致此错误,如果脚本在Firebase之前加载。例如,当使用Firebase Hosting时,它们将创建以下标签:
<script defer src="/__/firebase/6.0.2/firebase-app.js"></script>
...
<script defer src="/__/firebase/init.js"></script>
然后您还需要使用defer加载脚本
<script defer src="js/app.js"></script>