Этот проект несколько Известно из курса Udemy. Я получаю поддержку сообщений как неопределенная при попытке использовать FS, Path и Grey Matter для получения метаданных и текста из файла разметки внутри Root/postdb.
Это файл util с Функции, которые извлекают файлы MD: < /p>
posts-util.ts
Код: Выделить всё
import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
const postsDirectory = path.join(process.cwd(), 'postsDB');
console.log("Posts directory path:", postsDirectory);
if (!fs.existsSync(postsDirectory)) {
console.error("Posts directory does not exist:", postsDirectory);
}
function getPostData(fileName: string) {
const filePath = path.join(postsDirectory, fileName);
const fileContent = fs.readFileSync(filePath, 'utf-8');
const { data, content } = matter(fileContent);
const postSlug = fileName.replace(/\.md$/, '');
const postData = {
slug: postSlug,
date: data.date,
isFeatured: data.isFeatured || false,
...data,
content,
};
return postData;
}
export function getAllPosts() {
const postFiles = fs.readdirSync(postsDirectory);
console.log("Post files:", postFiles);
const allPosts = postFiles.map(postFile => {
return getPostData(postFile);
});
console.log("All posts:", allPosts);
const sortedPosts = allPosts.sort((postA, postB) => (postA.date > postB.date ? -1 : 1));
return sortedPosts;
}
export function getFeaturedPosts() {
const allPosts = getAllPosts();
console.log("All posts in getFeaturedPosts:", allPosts);
const featuredPosts = allPosts.filter(post => post.isFeatured);
console.log("Featured posts:", featuredPosts);
return featuredPosts;
}
< /code>
Это файл index.tsx (homepage) < /p>
import FeaturedPosts from "@/components/home-page/featured-posts";
import Hero from "@/components/home-page/hero";
import { Post } from "@/interfaces/Post";
import { getFeaturedPosts } from "@/lib/posts-util";
interface HomePageProps {
posts: Post[];
}
export default function HomePage({ posts }: HomePageProps) {
console.log("HomePage posts:", posts);
return (
);
}
export async function getStaticProps() {
console.log("getStaticProps called");
const featuredPosts = getFeaturedPosts();
console.log("Featured posts in getStaticProps:", featuredPosts); // Debugging line
return {
props: {
posts: featuredPosts,
},
revalidate: 1800,
};
}
< /code>
И это компоненты, которые визуализируют посты.import PostItem from "./post-item";
import { Post } from "@/interfaces/Post";
import styles from "@/styles/posts-grid.module.css";
interface PostsGridProps {
posts: Post[];
}
export default function PostsGrid({ posts = [] }: PostsGridProps) {
return (
[list]
{posts.map(post => (
))}
[/list]
);
}
< /code>
featured-posts.tsx
import PostsGrid from "../posts/posts-grid";
import { Post } from "@/interfaces/Post";
import styles from "@/styles/featured-posts.module.css";
interface FeaturedPostsProps {
posts: Post[];
}
export default function FeaturedPosts({ posts }: FeaturedPostsProps) {
console.log("FeaturedPosts posts:", posts);
return (
Featured Posts
);
}
https://github.com/rodhis/next-blog
Я думаю, что важно сказать, что нет консоли.log и console.error из постов-util.ts появляются на консоли! п>
Подробнее здесь: https://stackoverflow.com/questions/794 ... undefineds
Мобильная версия