Unhandled promise rejection: TypeError: _firebase.db.collection is not a function

Viewed 136

I'm new to react-native and JS. My project is a chat app and I'm trying to create button that onPress creating new chat, using the following function:

const createChat = async () =>{
    await db
    .collection("chats")
    .add({
        chatName: input,
    })
    .then(() => {
        navigation.goBack();
    })
    .catch((error) => alert(error));

}

this my firebase.js file:


import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/firestore';
import 'firebase/compat/database';


const firebaseConfig = {
    apiKey: "*",
    authDomain: "*",
    projectId: "*",
    storageBucket: "*",
    messagingSenderId: "*",
    appId: "*",
    measurementId: "*"
  };

  let app;

  if (firebase.apps.length === 0){
      app = firebase.initializeApp(firebaseConfig);
  } else {
        app = firebase.app();
  }

  const db = firebase.app();
  const auth = firebase.auth();

  export {db , auth};

enter image description here

1 Answers
const db = firebase.app(); // an instance of FirebaseApp

should be

const db = firebase.firestore(); // an instance of Firestore

Instead of learning using the compatibility library, consider learning the new Modular SDK instead as this will be the way forward.

Related