When I'm trying to save a snapshot of my query from firestore it returns as q: Query<DocumentData>
, and my query snapshot is querySnap: QuerySnapshot<DocumentData>
.
DocumentData type is: [field: string]: any;
And I can't loop through it without getting any errors.
my effect Code
useEffect(() => {
const fetchListings = async () => {
try {
// Get reference
const listingsRef = collection(db, "listings");
// Create a query
const q = query(
listingsRef,
where("type", "==", params.categoryName),
orderBy("timestamp", "desc"),
limit(10)
);
// Execute query
const querySnap = await getDocs(q);
let listings: DocumentData | [] = [];
querySnap.forEach((doc) => {
return listings.push({ id: doc.id, data: doc.data() });
});
setState((prevState) => ({ ...prevState, listings, loading: false }));
} catch (error) {
toast.error("Something Went Wront");
}
};
if (mount.current) {
fetchListings();
}
return () => {
mount.current = false;
};
}, [params.categoryName]);
Does anyone know how to set my listings type correctly?
The listings type from firestore should be:
type GeoLocationType = {
_lat: number;
_long: number;
latitude: number;
longitude: number;
};
export type ListingsDataType = {
bathrooms: number;
bedrooms: number;
discountedPrice: number;
furnished: boolean;
geolocation: GeoLocationType;
imageUrls: string[];
location: string;
name: string;
offer: boolean;
parking: boolean;
regularPrice: number;
timestamp: { seconds: number; nanoseconds: number };
type: string;
userRef: string;
};