Trouble Accessing Database. Firebase 9.0

I am currently working on a Udemy course (Complete React Developer in 2021) and we are trying to integrate Firebase/Firestore into an e-commerce project. Unfortunately the tutorial is using Firebase V6.10 and I wish to use the latest version 9.0.

I have managed to figure out how to initialise it and add Google authenticator but are now having problems querying data from the collection they had us create on cloud Firestore.

Here is the function I am trying to add that is causing my error:

> export const createUserProfileDocument = async(userAuth,
> aditionalData) => {
>       if (!userAuth) return;
>       const querySnapshot = await getDocs(collection(db, 'users'));
>       querySnapshot.forEach((doc) => {
>         console.log(`${} => ${}`);
>       });
>          }

This code gives me a failed to compile error and says that ‘db is not defined’.

I have tried defining db as a string of what I believe is the CollectionReference, ‘crwn-db-ff8b4’ in this instance. This gives me an error of

Unhandled Rejection (FirebaseError): Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore

I am very confused as to what exactly I need db’s value to be.

Here is a snapshot of my Firestore database page if that helps. enter image description here


I’m not sure where you have initialized Firestore but the error says db is undefined. Try adding this:

const db = getFirestore()

const querySnapshot = await getDocs(collection(db, 'users'));