Nextjs getting build time error Cannot read properties of undefined

Viewed 81

I am not getting any error when browsing page or url but when trying to npm run build getting this error Generating static pages (0/8)TypeError: Cannot read properties of undefined (reading 'blog_slug')

here is my full code:

I aslo want to show 404 page if dynamic url not exits. Right now I am getting this error for invalid url . I also set

if (res != 200) {
    return {
      notFound: true,
    };
  }

but didn't work. here is my full code:

export async function getStaticPaths() {
  const search_url = "https://backendapi.mydomain.com/blog-list/";
  const search_result = await fetch(search_url);
  const search_data = await search_result.json(search_result);
  const paths = search_data.map((data) => {
    return {
      params: {
        id: data.blog_slug,
      },
    };
  });
  return {
    paths,
    fallback: true, // false or 'blocking'
  };
}

export async function getStaticProps(context) {
  const id = context.params.id;
  const res = await fetch(`https://mydomain/blog-api/${id}`);
  const Blog = await res.json();
  const search_url = "https:/mydomain/blog-list/";
  const search_result = await fetch(search_url);
  const search_data = await search_result.json(search_result);

  if (res != 200) {
    return {
      notFound: true,
    };
  }

  return {
    props: {
      data: Blog,
      search_data: search_data,
    },
  };
}

here is my jsx look like:

function blog_details({ data, search_data }) {
      return (<h1>data.blog_slug<h1/)
}
0 Answers
Related