Geofirestore get subcollections from geo collection in react-native

472 Views Asked by At

I would like to retrieve the subcollections by making my request with geofirestore, like so:

My firestore

enter image description here

The id of each PRODUCTS corresponds to that of the user who created new products (for the moment there is only one).

That's my code right now:

import firestore from '@react-native-firebase/firestore';
import * as geofirestore from 'geofirestore';

const firestoreApp = firestore();
const GeoFirestore = geofirestore.initializeApp(firestoreApp);
const geocollection = GeoFirestore.collection('PRODUCTS');

const query = geocollection.limit(30).near({
  center: new firestore.GeoPoint(coords.latitude, coords.longitude),
  radius: 1000,
});

try {
  query.onSnapshot((querySnapshot) => {
    const productsQueried = querySnapshot.docs.reduce(
      (result, documentSnapshot) => {
        console.log(documentSnapshot);
        if (documentSnapshot.id !== user.uid) {
          result.push(documentSnapshot.data());
        }
        return result;
      },
      []
    );
    setListOfProducts(productsQueried);
    console.log(productsQueried);
    setLoading(false);
  });
} catch (error) {
  console.log(error);
}

Of course, I can only find the geocollection, but cannot access the 'USER_PRODUCTS' collection inside. {exists: true, id: "OUJ6r3aF9nVfgtfkQRES7kpYCko1", distance: 0, data: ƒ}

The final goal is to retrieve a list of products for each close customer and then sort so as not to retrieve that of the current user.

Do I necessarily have to make a second request (can I do it in one?) Or do I have to change the way I save the product lists of different users in firestore?

0

There are 0 best solutions below