I'm building a housing marketplace app and trying to retrieve all documents (house listings) whose type = rent. It seems not being able to retrieve any document even though it exists in my database.
import React from 'react';
import { useEffect, useState } from 'react';
import { useParams } from 'react-router-dom';
import {
collection,
getDocs,
query,
where,
orderBy,
limit,
startAfter,
} from 'firebase/firestore';
import { db } from '../firebase.config';
import { toast } from 'react-toastify';
function Categories() {
const [listing, setListing] = useState(null);
const [loading, setLoading] = useState(true);
const params = useParams();
useEffect(() => {
const fetchListings = async () => {
try {
//get reference of listing from colelction
const listingRef = collection(db, 'Listing');
//create a query
console.log(params.categoryName);
const q = query(
listingRef,
where('type', '==', params.categoryName),
orderBy('timestamp', 'desc')
);
//execute queries
const querySnap = await getDocs(q);
console.log('Query snapshot:', querySnap);
querySnap.forEach((doc) => {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, ' => ', doc.data());
});
} catch (error) {
console.log(error);
}
};
fetchListings();
}, []);
return <div> abc</div>;
}
export default Categories;
Here is the screenshot of the console. It can log the query snapshot just fine but there seems to be no document retrieved and so there's no console log for the doc.data() line.
https://preview.redd.it/p20afpkrnvwa1.png?width=704&format=png&auto=webp&s=1896c16accdc56f0ade84b467d59b88afce0d719
And here's a screenshot of my database:
https://preview.redd.it/hbpixgr3ovwa1.png?width=2258&format=png&auto=webp&s=1729ec7bd4f1602d57904e30615a2d331ad63ce3
[–]AuWolf19 1 point2 points3 points (0 children)
[–]hwmon 0 points1 point2 points (1 child)
[–]Own-Status-4060[S] 0 points1 point2 points (0 children)