0
votes

I'm migrating a simple html / javascript project from firebase realtime database to cloud firestore. I've followed the quickstart and added the library for firebase-firestore - but when I try to initialise the firestore I get 'Uncaught TypeError: firebase.firestore is not a function'. The quickstart mentions:

const firebase = require("firebase");
// Required for side-effects
require("firebase/firestore");

.. but I think that's only if I'm using npn to get the libraries, and doesn't apply?

What am I missing?

index.html

<head>
<script src="https://www.gstatic.com/firebasejs/4.5.1/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.5.1/firebase-firestore.js"></script>
<script src="./app.js" type="text/javascript"></script>

app.js

(function()

  {var config = {
  apiKey: "...",
  authDomain: "...",
  databaseURL: "...",
  projectId: "...",
  storageBucket: "...",
  messagingSenderId: "..."
  };

 firebase.initializeApp(config);

 var oldDB = firebase.database(); <-- THIS WORKS
 // Initialize Cloud Firestore through Firebase
 var newDB = firebase.firestore(); <-- 'Uncaught TypeError: firebase.firestore is not a function'
2

2 Answers

0
votes

The firestore sdk isn't included it the main firebase package. You need to import both:

import * as firebase from 'firebase' 

and

import 'firebase/firestore'
0
votes

The answer was that I was calling the javascript from another html page, where I hadn't updated the library references to include the one for firebase-firestore. When this was updated the firestore db initialisation worked fine..