How i get all subcollection in document in collection

i looking for document talk about this my problem but it look like quite quite a few

i have one collection like this collection(user1)=>uid=>document( collection(Product => uid ),nameUser.... bla bla)

i get all Product's document user1 this is quite easy but how i get this with user 2, user 3 and gathered into array product of all user?

i have tried this code in below but it doesn't understand

getData = async () => {
    const a = [];
    await firebase.firestore().collection('user').doc().collection('Product').onSnapshot(sanpham => {

      sanpham.forEach((doc) => {
        a.push({ id: doc.id, data: doc.data() })
      })
    })
    this.setState({ data: a });
  }

Upvotes: 2

Views: 300

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600090

If you want to get the documents from all Product collections, you can use a collection group query.

To use it, you'd do:

await firebase.firestore().collectionGroup('Product').onSnapshot(sanpham => {
  sanpham.forEach((doc) => {
    a.push({ id: doc.id, data: doc.data() })
  })
})

If you want to know which user a product belongs to, you can determine that with:

const productRef = doc.ref;
const productCollectionRef = productRef.parent;
const userRef = productCollectionRef.parent;
console.log(userRef.id); 

Upvotes: 2

Related Questions