firebase2.default.firestore is not a function - Re

2020-04-03 04:32发布

I am trying to use firestore in my reactjs app.

When i call firestore from firebase config object.

It says

firebase2.default.firestore is not a function

Here is the code

import firebase from 'firebase'

const config = {
  apiKey: "AIzaSxxxxxxqpUeqKXI",
    authDomain: "payxxxxxa1.firebaseapp.com",
    databaseURL: "https://payxxxxxxxa1.firebaseio.com",
    projectId: "payxxxxxxja1",
    storageBucket: "payxxxxxa1.appspot.com",
    messagingSenderId: "281xxxxxxxx576"
}

firebase.initializeApp(config)

export const fire = firebase
export const ref = firebase.database().ref()
export const firebaseAuth = firebase.auth
export const messaging = firebase.messaging();
export const db = firebase.firestore();

Firebase version is 4.5.0

npm list firebase > firebase@4.5.0

Am i doing wrong?

4条回答
劫难
2楼-- · 2020-04-03 04:46

I fixed it by importing multiple libraries: firebase and firebase/firestore. That's because the firebase core library does not include the firestore library innately. So the full code to fix it is:

import * as firebase from 'firebase';
import 'firebase/firestore';
查看更多
对你真心纯属浪费
3楼-- · 2020-04-03 04:47

ES6:

import '@firebase/firestore'

reference: https://www.npmjs.com/package/@firebase/firestore

查看更多
forever°为你锁心
4楼-- · 2020-04-03 05:03

I have resolved this issue by using require('firebase/firestore')

查看更多
Juvenile、少年°
5楼-- · 2020-04-03 05:04

If you're using the official firebase npm package, this is how you import it.

import firebase from 'firebase';
import 'firebase/firestore';
查看更多
登录 后发表回答