I have the problem that when I have reached the end of my data, Mongoose continues to provide me with data.If I constantly set the limit to 11, everything works. But I want to create the limit dynamically.
export async function POST(request) {
const body = await request.json();
try {
mongoConection();
} catch (error) {
console.log(error);
}
const products = await Product.find({})
.sort({
createdAt: -1,
})
.skip(body.skip)
.limit(body.limit);
return NextResponse.json({ products });
}
MainImage.js
const [page, setPage] = useState(0);
const fetchProducts = async () => {
setLoadMore(true);
const fetching = await FetchInitProducts(
page,
sessionStorage.getItem("currentLimit")
? sessionStorage.getItem("currentLimit")
: 11
);
useEffect(() => {
fetchProducts();
}, [page]);
<SingleImage isLast={index === result.length - 1} newLimit={() => {
setPage(page + 11);sessionStorage.setItem("currentLimit", page + 11);}}
product={product} handleClickOnImageEvent={handleClickOnImageEvent}/>
SingleImage.js:
export default function SingleImage({
handleClickOnImageEvent,
product,
newLimit,
isLast,
}) {
useEffect(() => {
if (!productRef?.current) return;
const observer = new IntersectionObserver(([entry]) => {
if (isLast && entry.isIntersecting) {
newLimit();
observer.unobserve(entry.target);
}
});
observer.observe(productRef.current);
}, [isLast]);
...
}