0

i'm getting this error when i run npm run build.

Error occurred prerendering page "/components/comments". Read more: https://nextjs.org/docs/messages/prerender-error TypeError: Cannot destructure property 'user' of 'props' as it is undefined.

This is how i'm passing it from parent to child

<CommentComponent 
props={{
            user,
            myComments,
            currentComments,
            value,
            setMsg,
            text,
            setText,
          }}
/>

And this is how i'm receiving it

interface CommentComp {
  props: {
    user: User;
    myComments: MyComments[];
    currentComments: (data: User) => void;
    value: Date;
    setMsg: (data: string) => void;
    text: string;
    setText: (data: string) => void;
  };
}

export default function CommentComponent({ props }: CommentComp): JSX.Element {

  const { user, myComments, currentComments, value, setMsg, text, setText } =
    props;

}

It works fine when running it locally.

I'm getting the props as

export const getServerSideProps: GetServerSideProps = async (context) => {
  const { email } = context.query as { email: string };

  const fetchData = await fetch(`http://localhost:3000/api/user/${email}`);

  const fetchResult = await fetchData.json();

  try {
    const userData = fetchResult as {
      currUser: User;
      notes: MyNotes[];
      tasks: MyTasks[];
      comments: MyComments[];
      reminders: MyReminder[];
    };

    return {
      props: {
        user: userData.currUser,
        notes: userData.notes,
        tasks: userData.tasks,
        comments: userData.comments,
        reminders: userData.reminders,
      },
    };
  } catch (error) {
    console.log(error);
  }
};

After the user signed in it redirects to the [email].tsx page

José Carlos
  • 626
  • 3
  • 23
  • 1
    What you've shown will work. The problem must be triggered by some other call to `CommentComponent`. (That said, I **really** wouldn't call a prop `props`, it's tremendously confusing.) – T.J. Crowder Oct 02 '22 at 11:42
  • 1
    [Working here](https://stackblitz.com/edit/react-ts-f58urw?file=index.tsx). That's not using Next.js, but the above is just straight-up TypeScript and React. Again, there must be some other use of `CommentComponent` causing the error. – T.J. Crowder Oct 02 '22 at 11:50
  • Maybe the problem is how i'm getting all of those props. It's coming from a GetServerSideProps. I'll edit my question, could you check it one more time? – José Carlos Oct 02 '22 at 14:03
  • Is `CommentComponent` meant to be a page? Only page components can live under the `pages` folder as Next.js routing depends on its structure. See [Can't build React/Next project - found page without a React Component as default export (context api file)](https://stackoverflow.com/questions/65598753/cant-build-react-next-project-found-page-without-a-react-component-as-default). – juliomalves Oct 03 '22 at 23:56

0 Answers0