adrianhajdin/ecommerce

Hi, please i need help for resolving this error

Kapelo256 opened this issue · 11 comments

⨯ src\app_components\Categories\index.tsx (18:28) @ map
⨯ TypeError: Cannot read properties of null (reading 'map')
at Categories (./src/app/_components/Categories/index.tsx:49:38)
at stringify ()
16 |
17 |

18 | {categories.map((category) => (
| ^
19 |
20 | ))}
21 |

Provide the code snipped for it .from above error either the type for the categories is null or data is not loaded in your file.

The error is because the categories prop is null. Make sure the categories prop is not null and check whether the error remains or not by adding below snippet during the map array function
{categories?.map((category) => (

))}

The categories props comes from payload-types.ts make sure everything is correct there. The usage of optional chaining might resolve the issue which is {categories?.map((category) => ())}, if it doesn't then there might be another codebase which causes the error.

I have the same problem. I tried two times two repeat the whole chapter but lways : ⨯ src/app/_components/Categories/index.tsx (17:21) @ map
⨯ TypeError: Cannot read properties of null (reading 'map')
at Categories (./src/app/_components/Categories/index.tsx:49:38)
at stringify ()
digest: "2181736854"
15 |


16 |

17 | {categories.map((category) => (
| ^
18 |
19 | ))}
20 |

At least the page will show when I add the ? like you said but this still wont give me any categories on my page

So I found my mistake. I was missing the fourth closing curley bracket in the categories .ts in line 24. After adding it I got another error because of missing images in some category cards I created so I also added the question mark to line 22 from the categoryCard index.tsx to make the image optional: style={{ backgroundImage: url(${media?.url}) }}. Of course you don't necessarily want this when the product is running but it helps building before you got all your images together.

I was facing the same challenge and followed the steps as provide above, now I have no error but the categories and images are not showing. Someone help me

I had the same problem. I was missing the last "t" letter in height world in the categories.ts
I recommende chechking the modified files in the category section:
src\payload\collections\Categories.ts
src\payload\payload-types.ts
src\app_graphql\products.ts
src\app_graphql\categories.ts
src\app_graphql\blocks.ts
src\app(pages)[slug]
src\app_api\fetchDocs
src\app_components\Categories

Hello everyone, I also had a problem with this section, and after a few attempts, I discovered what was wrong with me.

In the (pages)[slug]page.tsx file, I needed to add the search for categories in the try/catch block.

In my specific case, after entering this data everything worked as expected.

   try {
     page = await fetchDoc<Page>({
       collection: 'pages',
       slug,
       draft: isDraftMode,
     })

     categories = await fetchDocs<Category>('categories')
   } catch (error) {
     console.error(error)
   }

I hope it helps anyone who is going through the same difficulty.

Best regards.

Thanks Adrian for another excellent tutorial.

Let's Code!!