Home > other >  React access to firestore
React access to firestore

Time:01-24

Hi the below code has to fetch a list of values ​​from firestore database, when i run the code i get the following error what should i do:

Error:

TypeError: firebase__WEBPACK_IMPORTED_MODULE_7_.db.collection is not a function

firebase.js

import { initializeApp } from 'firebase/app';
import { getFirestore } from 'firebase/firestore';

var firebaseConfig = {
  apiKey: 'xx',
  authDomain: 'xx',
  projectId: 'xx',
  storageBucket: 'xx',
  messagingSenderId: 'xx',
  appId: 'xx',
  measurementId: 'G-xxxx',
};

// Initialize Firebase
var app = initializeApp(firebaseConfig);
export const db = getFirestore(app);


 try {
      console.log("recupero lista news_profiles");
      db.collection('news_profiles')
        .get()
        .then((querySnapshot) => {
          console.log(querySnapshot);
          const data = [];
          querySnapshot.forEach((doc) => {
            console.log(doc.id, ' => ', doc.data());
            data.push(doc.data());
          });
          console.log(data);
        })
        .catch((error) => {
          console.log('Error getting documents: ', error);
        });
    } catch (error) {
      console.log('Error: '   error);
    }

CodePudding user response:

You have mixed namespaced and modular imports.

Here is an example.

import { collection, getDocs } from "firebase/firestore"; 

const querySnapshot = await getDocs(collection(db, "users"));
querySnapshot.forEach((doc) => {
  console.log(`${doc.id} => ${doc.data()}`);
});

Ensure when reading the docs you are following “web version 9” guides.

  • Related