简体   繁体   中英

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

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};

在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM