• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 Next.js 게시판 not-found.js 관련

not-found.js 관련

2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 글쓴이
  • #93540

    이현성
    참가자
    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>
      );
    }
     
    #93555

    codingapple
    키 마스터
    params.articleId 에 이상한 공백같은건 없나 _id 형식은 맞나 출력해봅시다
2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠