javascript - Firebase: TypeError: firebase.firestore is not a function - Stack Overflow

I'm following the example at So I have this in my index.html<body><!-- The core Firebase

I'm following the example at

So I have this in my index.html

<body>
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src=".2.4/firebase-app.js"></script>

<!-- include only the Firebase features as you need -->
<script defer src=".2.4/firebase-auth.js"></script>
<script defer src=".2.4/firebase-firestore.js"></script>
<script defer src=".2.4/firebase-messaging.js"></script>

<!-- Initialize Firebase -->
<script src="./firebase-init.js"></script>
</body>

and this in firebase-init.js

// Your web app's Firebase configuration
var firebaseConfig = {
    apiKey: ...
    authDomain: ...
    databaseURL: ...
    ...
};

// Initialize Firebase
var app = firebase.initializeApp(firebaseConfig);

// Initialize Firestore
var db = firebase.firestore();
// also tried: 
// var db = firebase.firestore(app);
// var db = app.firestore();

And yet whenever I try to load the page I get TypeError: firebase.firestore is not a function

This seems so trivial but I cannot get access to the firestore

I'm following the example at https://firebase.google./docs/web/setup

So I have this in my index.html

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

<!-- include only the Firebase features as you need -->
<script defer src="https://www.gstatic./firebasejs/6.2.4/firebase-auth.js"></script>
<script defer src="https://www.gstatic./firebasejs/6.2.4/firebase-firestore.js"></script>
<script defer src="https://www.gstatic./firebasejs/6.2.4/firebase-messaging.js"></script>

<!-- Initialize Firebase -->
<script src="./firebase-init.js"></script>
</body>

and this in firebase-init.js

// Your web app's Firebase configuration
var firebaseConfig = {
    apiKey: ...
    authDomain: ...
    databaseURL: ...
    ...
};

// Initialize Firebase
var app = firebase.initializeApp(firebaseConfig);

// Initialize Firestore
var db = firebase.firestore();
// also tried: 
// var db = firebase.firestore(app);
// var db = app.firestore();

And yet whenever I try to load the page I get TypeError: firebase.firestore is not a function

This seems so trivial but I cannot get access to the firestore

Share Improve this question edited Jul 7, 2019 at 15:20 Simon H asked Jul 7, 2019 at 15:14 Simon HSimon H 21.1k14 gold badges81 silver badges142 bronze badges 5
  • Why do you have var db = app.firestore() as a ment? Did you try it? – mahmoudafer Commented Jul 7, 2019 at 15:18
  • yes, various permutations based on ideas in the docs, but always the same end result – Simon H Commented Jul 7, 2019 at 15:21
  • 2 Any reason why you've chosen not to put defer in your firebase-init.js include? Seems like you'd want it to execute in serial with the other Firebase scripts so everything gets executed in the same order, predictably. – Doug Stevenson Commented Jul 7, 2019 at 15:21
  • aha - that seems to be the issue. Adding a defer helped, but ran into issues with the rest of my code; removing them all seems to take me forward even further – Simon H Commented Jul 7, 2019 at 15:25
  • @DenzDacs what about react native people who are getting the same error ?? – kazim ali Commented Sep 15, 2021 at 12:58
Add a ment  | 

3 Answers 3

Reset to default 2

Did you try adding a defer here?

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

Based on documentation I would assume that you should do this.

Maybe the issue is with the new version. Try using a older version of Firebase

npm install [email protected] --save

You have to import:

import { getFirestore, collection, getDocs } from 'firebase/firestore/lite';

While initializing:

const db = getFirestore(app);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745336072a4623110.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信