0

Below are my security rules for firestore database:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

Below is my code to fetch data:

fetchUsers=async()=>{
        const db=firebase.firestore();
        const response=db.collection('users');
        const data=await response.get();
        data.doc.forEach(item=>{console.log(item)})
    }

users is the name of the collection in the firestore. It has one document.

But I get an error on console:

Cannot read forEach property of undefined

what am I missing? Please help me to find the solution of above problem.

Thank You!

2 Answers 2

1

You must use either of the following

import { useParams } from 'react-router-dom'

function YYY(props){
    const { id } = useParams();
    db.collection('users').doc(id).get().then((doc) =>{console.log(doc.data())})
}

Or just use onSnapshot

db.collection('users').onSnapshot((snapshot) => {
    snapshot.docs.map((doc) => {
        console.log(doc.data());
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, its working. But the code I have shown in the question was taken from a tutorial. why it isn't working?
0

Your code does not work because there is no doc property for a QuerySnapshot. The property which returns an array of all the documents in the QuerySnapshot is named docs.

So you need to do data.docs.forEach(...);

You probably made a typo while copying the code from the tutorial.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.