简体   繁体   English

未捕获的ReferenceError:未定义firebase

[英]Uncaught ReferenceError: firebase is not defined

I am trying to create Google authentication but I copy var provider = new firebase.auth.GoogleAuthProvider(); 我正在尝试创建Google身份验证,但我复制了var provider = new firebase.auth.GoogleAuthProvider(); to my JavaScript and it keeps telling me that Uncaught ReferenceError: firebase is not defined . 到我的JavaScript,并不断告诉我Uncaught ReferenceError: firebase is not defined

Here is my code: 这是我的代码:

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;

}); 
}

You are getting this error because firebase variable has not been instantiated and initialized on the page on which you are running the code. 您收到此错误是因为尚未在运行代码的页面上实例化和初始化firebase变量。 To do so you need to - 为此,您需要-

  1. 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.js包含代表您的Firebase应用程序的变量firebase firebase-auth.js contains the firebase.auth library which your code is using. firebase-auth.js包含firebase.auth你的代码使用库。

  1. 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 要了解更多信息,请参阅-https: //firebase.google.com/docs/web/setup

One common gotcha with Firebase that would cause this error is if your script loads before Firebase. Firebase的一个常见陷阱会导致此错误,如果脚本在Firebase之前加载。 When using Firebase Hosting for example they create these tags: 例如,当使用Firebase Hosting时,它们将创建以下标签:

  <script defer src="/__/firebase/6.0.2/firebase-app.js"></script>
  ...
  <script defer src="/__/firebase/init.js"></script>

You need to then also load your script using defer 然后,您还需要使用defer加载脚本

  <script defer src="js/app.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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