未捕获的ReferenceError:未为Web定义firebase

时间:2020-07-27 23:03:36

标签: javascript firebase firebase-authentication

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Welcome to Firebase Hosting</title>

<!-- update the version number as needed -->
<script defer src="/__/firebase/7.17.1/firebase-app.js"></script>
<!-- include only the Firebase features as you need -->
<script defer src="/__/firebase/7.17.1/firebase-auth.js"></script>
<script defer src="/__/firebase/7.17.1/firebase-database.js"></script>
<script defer src="/__/firebase/7.17.1/firebase-messaging.js"></script>
<script defer src="/__/firebase/7.17.1/firebase-storage.js"></script>
<!-- initialize the SDK after all desired features are loaded -->
<script defer src="/__/firebase/init.js"></script>
<script defer src="https://www.gstatic.com/firebasejs/ui/4.6.0/firebase-ui-auth.js"></script>
<link type="text/css" rel="stylesheet" href="https://www.gstatic.com/firebasejs/ui/4.6.0/firebase-ui-auth.css" />
<script defer>
  var uiConfig = {
    signInSuccessUrl: 'main.html',
    signInOptions: [
        firebase.auth.GoogleAuthProvider.PROVIDER_ID,
        firebase.auth.EmailAuthProvider.PROVIDER_ID
    ],
    tosUrl: '<your-tos-url>',
    // Privacy policy url/callback.
    privacyPolicyUrl: function() {
      window.location.assign('<your-privacy-policy-url>');
    }
  };
  var ui = new firebaseui.auth.AuthUI(firebase.auth());
  ui.start('#firebaseui-auth-container', uiConfig);
</script>

在浏览器中打开页面时,出现以下错误Uncaught ReferenceError: firebase is not defined。我不确定如何解决此问题。我正在使用保留的托管网址来下载Firebase JS SDK。我的理解是,您不需要在其他文章中看到的config变量和initializeApp()方法,对吗?我真的不确定发生了什么。我尝试将脚本插入文档的正文和标题中。我什至尝试使用在body元素底部插入的单独的js文件。

更新。

我将代码放在正文底部的登录脚本中,并将其与一些检查auth状态的代码结合在一起。看起来像这样

<script>
  document.addEventListener('DOMContentLoaded', function() {
    firebase.auth().onAuthStateChanged(user => {console.log("user: " + user);});
    // firebase.database().ref('/path/to/ref').on('value', snapshot => { });
    // firebase.messaging().requestPermission().then(() => { });
    // firebase.storage().ref('/path/to/ref').getDownloadURL().then(() => { });
  });
  var uiConfig = {
    signInSuccessUrl: 'main.html',
    signInOptions: [
      firebase.auth.GoogleAuthProvider.PROVIDER_ID,
      firebase.auth.EmailAuthProvider.PROVIDER_ID
    ],
    tosUrl: '<your-tos-url>',
    // Privacy policy url/callback.
    privacyPolicyUrl: function() {
      window.location.assign('<your-privacy-policy-url>');
    }
  };
  var ui = new firebaseui.auth.AuthUI(firebase.auth());
  ui.start('#firebaseui-auth-container', uiConfig);
</script>

console.log("user: " + user)语句显示用户:null,表明已定义firebase中的addEventListner()对象。然而,firebase.auth.GoogleAuthProvider.PROVIDER_ID,行引发的错误与之前相同。为什么?

1 个答案:

答案 0 :(得分:0)

我无法解决问题。最后,我选择不使用FirebaseUI,而是创建自己的登录流程。