I'm trying to fetch the shop and it's coupons, I have two model one for the shop and one for the coupon, also two routers, one for fetching shops and one for fetching coupons, the shops are fetching fine and showing in client side, but the coupons are not showing in the client side. When /coupons/${shopName}
I try it in postman it works fine, but in the client side not, I don't know why. Console log is giving me [object Object]
export default function ShopPage() {
const [shop, setShop] = useState("");
const shopName = useParams().shopName;
const [coupons, setCoupons] = useState([]);
useEffect(() => {
const fetchShop = async () => {
const res = await axios.get(`/shops/${shopName}`);
setShop(res.data);
console.log(res.data);
};
fetchShop();
}, [shopName]);
useEffect(() => {
const fetchShopCoupons = async () => {
const response = await axios.get(`/coupons/${shopName}`);
setCoupons(response.data);
console.log("Shop Coupons are:" + response.data);
};
fetchShopCoupons();
}, []);
return (
<>
<Box>
<Stack>
<Stack >
<Avatar alt={(shop.shopName)}
src={shop.shopPic}/>
<Stack>
<Box>
<Typography>
{shop.shopName}
</Typography>
</Box>
</Box>
</Stack>
</Stack>
<Box>
<Coupons coupons={coupons}/>
</Box>
</Stack>
</Box>
</>
)
}
Coupons Component:
export default function Coupons({ coupons = [] }) {
const [filteredResults, setFilteredResults] = useState([]);
const [searchInput, setSearchInput] = useState('');
const [isLoading, setIsLoading] = useState(false);
const filter = (e) => {
const keyword = e.target.value;
if (keyword !== '') {
const filteredData = coupons.filter((coupon) => {
return Object.values(coupon)
.join('')
.toLowerCase()
.includes(searchInput.toLowerCase())
})
setFilteredResults(filteredData)
} else {
setFilteredResults(coupons);
}
setSearchInput(keyword);
}
console.log("filtered Coupons are:", filteredResults);
return (
<div className="coupons">
<div className="couponsContainer">
<div className="couponsSearchContainer">
<div className="couponsSearch">
<div class="couponsSearchIconContainer">
<SearchIcon class="w-5 h-5" />
</div>
<input type="text"
className="couponsSearchInput"
placeholder="بحث"
name="couponSearchText"
id="couponSearchText"
onChange={filter}
/>
</div>
{/* ENDS OF COUPONSSEARCHCONTAINER */}
</div>
{/* ENDS OF COUPONSSEARCH */}
<div className="couponsBox">
{isLoading ? (
<Box sx={{ display: 'flex' }}>
<CircularProgress />
</Box>
) : (
filteredResults.length > 0 ? (
filteredResults.map((f) => (
<Coupon coupon={f} />
))
) : (
coupons.sort((a, b) =>
new Date(b.createdAt) - new Date(a.createdAt))
.map((c) => (
<Coupon coupon={c} />
)))
)
}
</div>
{/* ENDS OF COUPONSBOX */}
</div>
{/* ENDS OF COUPONSCONTAINER */}
</div>
//ENDS OF COUPONS
);
}