url์์ ๊ฒ์๊ธ id๋ฅผ ์กฐ์ํด ์ผ๋ถ๋ฌ 404 ํ์ด์ง๋ฅผ ์ ๋ํ ๋
์กฐ์๋ id๊ฐ ์งง๊ฑฐ๋ ์๋ชป๋ ํ์
์ผ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ์ ์๋ฌ๊ฐ ๋๋ ๊ฒ ๊ฐ์ต๋๋ค.
Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer
์๋ง ๋ชฝ๊ณ ๋๋น์ ObjectId์์ ๋ง๋ค์ด๋ด๋ ์๋ฌ์ธ ๊ฒ ๊ฐ์์ try - catch ๋ฌธ์ผ๋ก ์์ ํด๊ฒฐํด ๋ดค์ต๋๋ค.
์ด ๋ฐฉ๋ฒ์ด ์ ์ ํ ๊ฑด๊ฐ์?
import { connectDB } from "@/util/db";
import { ObjectId } from "mongodb";
import Comment from "@/components/Comment";
import { getServerSession } from "next-auth";
import { authOptions } from "@/pages/api/auth/[...nextauth]";
import GaeChuButton from "@/components/GaeChuButton";
import style from "./Detail.module.scss";
import common from "@/app/common.module.scss";
export default async function Detail({ params }) {
const session = await getServerSession(authOptions);
const db = (await connectDB).db("next-inside");
let article;
try {
article = await db
.collection("article")
.findOne({ _id: new ObjectId(params.articleId) });
if (!article) {
return <div>์กด์ฌํ์ง ์๋ ๊ฒ์๊ธ์
๋๋ค.</div>;
}
} catch {
return <div>์กด์ฌํ์ง ์๋ ๊ฒ์๊ธ์
๋๋ค.</div>;
}
return (
<div className={style.Detail}>
<div
className={`${style.GallView} ${common.CutCard} ${common["CutCard--Bottom"]}`}
>
<div className={style.StickyWrap}>
<header className={style.GallViewHead}>
<h2 className={style.GallViewTitle}>{article.title}</h2>
<p className={style.GallViewAuthor}>{article.author}</p>
</header>
<p className={style.GallViewContent}>{article.content}</p>
</div>
<GaeChuButton parent={params.articleId} session={session} />
</div>
<Comment parent={params.articleId} />
</div>
);
}