LoginSignup
44
17

More than 3 years have passed since last update.

firebaseのデータベースを使うときにUncaught TypeError: firebase.database is not a functionと吐かれた時の解決法

Last updated at Posted at 2019-07-11

しがない地方大学院生をしています、Sotaです。
プログラミング初心者であるため、内容に誤りがあるかもしれません。
もし、誤りがあれば修正するのでどんどん指摘してください。

今回は、firebaseでホームページを作るときに、firebase.database();でエラーを吐かれた時の解決策のメモです。

Uncaught TypeError: firebase.database is not a function

databaseに問い合わせのデータを格納しようとjsを書いていたら、コンソールに見出しのエラーを吐かれました。
色々なサイトを参考に初期設定から立ち上げいていたのですが、このエラーに関する情報がなかなか無くて詰んでしまいました。
その時の解決法です。
firebaseで最初に


<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/6.2.4/firebase-app.js"></script>

<!-- TODO: Add SDKs for Firebase products that you want to use
     https://firebase.google.com/docs/web/setup#config-web-app -->

<script>
  // Your web app's Firebase configuration
  var firebaseConfig = {
    apiKey: "***",
    authDomain: "***",
    databaseURL: "***",
    projectId: "***",
    storageBucket: "",
    messagingSenderId: "***",
    appId: "***"
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
  </script>

このコードをindex.htmlに書き込むと思います。
この部分に少しコードを加えると解決できました。


<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/6.2.4/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/6.2.4/firebase-auth.js"></script>  
<script src="https://www.gstatic.com/firebasejs/6.2.4/firebase-database.js"></script>

このように下の2行を追加すると解決しました。
auth.jsやdatabase.jsが読み込めていなかったのかもしれません。
参考にしたサイトは
https://stackoverflow.com/questions/38248723/firebase-database-is-not-a-function
こちらです。

44
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
44
17