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

前端 未结 21 1459
独厮守ぢ
独厮守ぢ 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: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
            });
    }
    

提交回复
热议问题