adrianhajdin / social_media_app

Build a modern social app with a stunning UI with a native mobile feel, a special tech stack, an infinite scroll feature, and amazing performance using React JS, Appwrite, TypeScript, and more.
https://jsmastery.pro
1.07k stars 322 forks source link

Getting error in useGetPosts. #15

Open aak-301 opened 8 months ago

aak-301 commented 8 months ago

Since this Api was most crucial, so I wrote what was shown in video. But sill I was getting this. I tried the doc. But wasn't able to figure out, where I was wrong. Can anyone help me with this.

This is in src>lib>react-queries>queries.ts

export const useGetPosts = () => { return useInfiniteQuery({ queryKey: [QUERY_KEYS.GET_INFINITE_POSTS], queryFn: getInfinitePosts, getNextPageParam: (lastPage) => { // If there's no data, there are no more pages. if (lastPage && lastPage.documents.length === 0) { return null; }
// Use the $id of the last document as the cursor. const lastId = lastPage?.documents[lastPage.documents.length - 1].$id; return lastId; }, }); };

The error message from vercel:

image
s3xmaxman commented 8 months ago
 export const useGetPosts = () => {
  return useInfiniteQuery({
    queryKey: [QUERY_KEYS.GET_INFINITE_POSTS],
    queryFn: getInfinitePosts as any,
    getNextPageParam: (lastPage: any) => {
      if (lastPage && lastPage.documents.length === 0) {
        return null;
      }
      const lastId = (lastPage.documents[lastPage.documents.length - 1].$id);
      return lastId;
    },
    initialPageParam: null,
  });
}

If you pass null to initialPageParam, the error should disappear

yung-slime commented 7 months ago

@s3xmaxman could you explain why it is so? just want to know.

s3xmaxman commented 7 months ago

@yung-slime In useInfiniteQuery, you can specify page parameters for pagination. Normally you would specify some value for the initial page. In most cases, the initial page can be fetched without any parameters, so you specify null. By doing so, no parameters will be passed to queryFn on the first query execution. On subsequent paging, the return value of getNextPageParam will be used as the page parameter.

yung-slime commented 7 months ago

you @s3xmaxman see how much it could help if you read the docs carefully.