firebase.firestore() is not a function when trying to initialize Cloud Firestore

前端 未结 21 1337
独厮守ぢ
独厮守ぢ 2020-12-01 02:45

When I try to initialize Firebase Cloud Firestore, I ran into the following error:

Uncaught TypeError: WEBPACK_IMPORTED_MODULE_0_firebase

相关标签:
21条回答
  • 2020-12-01 03:09

    Hope this helps to someone who faces the same problem when deploying an Angular app to Firestore.

    In Angular 8 project, I had the same error when deploying to Firestore. I fixed it by adding another module AngularFirestoreModule.

    App.module.ts is like this:

    ...
    import { AngularFireModule } from '@angular/fire';
    import { AngularFirestore, AngularFirestoreModule } from '@angular/fire/firestore'; // << Note AngularFirestoreModule !!!
    import { AngularFireDatabaseModule } from '@angular/fire/database';
    ...
    
    imports: [
        BrowserModule,
        FormsModule,
        AngularFireModule.initializeApp(environment.firebaseConfig),
        AngularFirestoreModule,
        AngularFireDatabaseModule,
    ...
    

    package.json:

    ...
    "dependencies": {
        "@angular/animations": "~8.2.2",
        "@angular/common": "~8.2.2",
        "@angular/compiler": "~8.2.2",
        "@angular/core": "~8.2.2",
        "@angular/fire": "^5.2.1",
        "@angular/forms": "~8.2.2",
        "@angular/platform-browser": "~8.2.2",
        "@angular/platform-browser-dynamic": "~8.2.2",
        "@angular/router": "~8.2.2",
        "ajv": "^6.10.2",
        "bootstrap-scss": "^4.3.1",
        "core-js": "^2.5.4",
        "firebase": "^6.4.0",
    ...
    

    UPDATE: When I deployed to some other hosting provider, this did not help. For this case, I added the original firebase library and it worked.

    import { firestore } from 'firebase'; 
    
    0 讨论(0)
  • 2020-12-01 03:12

    To use Firestore cloud functions on Node.js you should use admin.firestore() instead of admin.database(). Also you should be sure that your module firebase-admin on package.json is up to 5.4.1 or above. Looking like something like:

    {
      "name": "functions",
      "description": "Cloud Functions for Firebase",
      "dependencies": {   
        "firebase-admin": "^5.4.1",
        "firebase-functions": "^0.7.0"
      }
    }
    
    0 讨论(0)
  • 2020-12-01 03:14

    In my case the problem was that I've accidentally added firebase-admin alongside with the firebase package in my package.json. And firebase-admin took precedence so even after adding the imports as per the accepted answer I was still getting the same error.

    Removing the redundant firebase-admin from the client app fixed the problem.

    As a note, firebase-admin is intended for server side to operate in admin mode, while the firebase package is a client side library.

    0 讨论(0)
  • 2020-12-01 03:15

    To use firestore you need to add this link at the top of your html page.

    //After initializing firebase, in your main javascript page call...
    var data = firebase.firestore();
    <script src="https://www.gstatic.com/firebasejs/7.1.0/firebase-firestore.js"></script>
    <script src="https://www.gstatic.com/firebasejs/6.2.3/firebase-storage.js"></script>

    0 讨论(0)
  • 2020-12-01 03:16

    You can create a separate component for initialization of firebase on you application using credentials.

    firebase-context.js

    import * as firebase from 'firebase'
    import 'firebase/firestore';
    
    const firebaseConfig = {
                apiKey: "XXXX",
                authDomain: "XXXXX.firebaseapp.com",
                databaseURL: "https://XXXX-app-web.firebaseio.com",
                projectId: "XXXX",
                storageBucket: "XXXX-app-web.appspot.com",
                messagingSenderId: "XXXXXX",
                appId: "1:XXX:web:XXXX",
                measurementId: "G-XXXX"
            };
    
    export default !firebase.apps.length 
      ? firebase.initializeApp(firebaseConfig).firestore()
      : firebase.app().firestore();
    

    In case, you need to deal with database operation you don't need to call the initialize method each time, you can use common component method which is earlier created.

    import FirebaseContext from "./firebase-context";
    
    export const getList = () => dispatch => {
        FirebaseContext.collection("Account")
            .get()
            .then(querySnapshot => {
                // success code
            }).catch(err => {
                // error code
            });
    }
    
    0 讨论(0)
  • 2020-12-01 03:17

    First, make sure you have latest version of firebase:

    npm install firebase@4.12.0 --save
    

    Next, add both firebase and firestore:

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

    Initialize the firebase app:

    firebase.initializeApp({
      apiKey: '### FIREBASE API KEY ###',
      authDomain: '### FIREBASE AUTH DOMAIN ###',
      projectId: '### CLOUD FIRESTORE PROJECT ID ###'
    });
    
    // Initialize Cloud Firestore through Firebase
    var db = firebase.firestore();
    

    source: https://firebase.google.com/docs/firestore/quickstart?authuser=0

    0 讨论(0)
提交回复
热议问题