0

I received this Server Error from a Post.js file from a blog site i'm building and can't figure out how to fix it. After a lengthy search/investigation i'm unable to find anything to help correct it so if anyone can point me in the right direction to do so it'll be truly appreciated by this NextJS novice guy. Thank you!

TypeError: Cannot read properties of undefined (reading 'slug')

const Post = ({
  10 |   post: {
> 11 |     slug,
     |    ^
  12 |     content,
  13 |     frontMatter: { title, image, date, author, description },
  14 |   },

Post.js

import BlurImage from "@/components/BlurImage";
import { formatDate } from "@/utils/formatDate";
import { Calender, Clock } from "@/utils/Icons";
import { readingTime } from "@/utils/readingTime";
import { slugify } from "@/utils/slugify";
import { truncateString } from "@/utils/truncateString";
import Link from "next/link";

const Post = ({
  post: {
    slug,
    content,
    frontMatter: { title, image, date, author, description },
  },
  authors,
  compact,
  status,
}) => {
  return (
    <article className="bg-white d-flex flex-column h-100">
      {!compact && (
        <div className="post-image">
          <Link href={`/lotw/${slug}`} className="d-block" title={title}>
            <BlurImage
              className="w-100 h-auto"
              src={image}
              alt={`Hero image of Issue ${title}`}
              width="368"
              height="238"
            />
          </Link>
        </div>
      )}
      <div
        className={`p-4 ${authors != "current" ? "pb-0" : ""} ${
          status ? "position-relative" : ""
        }`}
      >
        {status && <p className="post-badge mb-0">{status}</p>}
        <ul className={`post-meta list-inline mb-3 ${status ? "mt-3" : ""}`}>
          <li className="list-inline-item">
            <Calender className="me-1 align-bottom" />
            {formatDate(date)}
          </li>
          <li className="list-inline-item">•</li>
          <li className="list-inline-item">
            <Clock className="me-1 align-bottom" />
            {readingTime(content)} min read
          </li>
        </ul>
        <div className="position-relative">
          <h3 className="h4 post-title mb-2 line-clamp clamp-2">
            <Link
              href={`/lotw/${slug}`}
              className="text-link stretched-link"
              title={title}
            >
              {title}
            </Link>
          </h3>
          <p className={`mb-0 line-clamp ${compact ? "clamp-2" : "clamp-3"}`}>
            {truncateString(description, 150)}
          </p>
        </div>
      </div>
      {authors != "current" &&
        (compact ? (
          <div className="post-author mt-auto p-4 pt-3">
            <Link
              href={`/author/${slugify(author)}`}
              className="is-hoverable"
              title={`Read all posts by - ${author}`}
            >
              {authors.map(
                (authorPage, key) =>
                  slugify(author) === authorPage.authorSlug && (
                    <span className="d-inline-block" key={key}>
                      <BlurImage
                        src={authorPage.authorFrontMatter.image}
                        alt={author}
                        className="w-auto"
                        width="26"
                        height="26"
                      />
                    </span>
                  )
              )}
            </Link>
            <span className="ms-3 me-2">by</span>
            <Link
              className="text-link"
              href={`/author/${slugify(author)}`}
              title={`Read all posts by - ${author}`}
            >
              {author}
            </Link>
          </div>
        ) : (
          <div className="post-author d-flex mt-auto p-4">
            <div className="flex-shrink-0">
              <Link
                href={`/author/${slugify(author)}`}
                className="is-hoverable"
                title={`Read all posts by - ${author}`}
              >
                {authors.map(
                  (authorPage, key) =>
                    slugify(author) === authorPage.authorSlug && (
                      <span className="d-inline-block rounded-circle" key={key}>
                        <BlurImage
                          src={authorPage.authorFrontMatter.image}
                          alt={author}
                          className="w-auto"
                          width="42"
                          height="42"
                        />
                      </span>
                    )
                )}
              </Link>
            </div>
            <div className="flex-grow-1 ms-3">
              <p className="mb-0 lh-base small">Written by</p>
              <Link
                className="text-link"
                href={`/author/${slugify(author)}`}
                title={`Read all posts by - ${author}`}
              >
                {author}
              </Link>
            </div>
          </div>
        ))}
    </article>
  );
};
export default Post;

slugify.js

export const slugify = (string) => {
  return string.trim().replace(/ /g, "-").toLowerCase();
};
Drew Reese
  • 165,259
  • 14
  • 153
  • 181
  • Looks like you forgot to add `getStaticProps` function. What you return from this function will be made available to your Page component. – GoodMan Jul 31 '23 at 07:52
  • What is the path to `Post.js`? Maybe you just have to rename the file to: `[slug].js` according to the docs: https://nextjs.org/docs/pages/building-your-application/routing/dynamic-routes – kzi Jul 31 '23 at 08:00
  • Path to the Post.js page is root/components/Post.js. Other component pages don't have getStaticProps export at the bottom on each component .JS file. Everything worked fine yesterday no errors. Ran npm run dev first thing and the error occurred. Monday! – anterrysocial Jul 31 '23 at 10:59

0 Answers0