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

home2 게시판 Node.js, Express 게시판 BSONError: Argument passed in must be a string of 12 bytes or a string of 24 hex

BSONError: Argument passed in must be a string of 12 bytes or a string of 24 hex

  • 이 주제에는 6개 답변, 4명 참여가 있으며 신재헌7 월 전에 전에 마지막으로 업데이트했습니다.
7 글 보임 - 1 에서 7 까지 (총 7 중에서)
  • 글쓴이
  • #99613

    채성수
    참가자
    node.js 강의를 보면서 따라치고 있는데 
    밑의 오류가 뜨면서
    
    
    D:\Node_JS\node_modules\bson\lib\bson.cjs:2055
                    throw new BSONError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer');
                          ^
    BSONError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer
        at new ObjectId (D:\Node_JS\node_modules\bson\lib\bson.cjs:2055:23)
        at D:\Node_JS\server.js:180:15
        at Layer.handle [as handle_request] (D:\Node_JS\node_modules\express\lib\router\layer.js:95:5)
        at next (D:\Node_JS\node_modules\express\lib\router\route.js:144:13)
        at Route.dispatch (D:\Node_JS\node_modules\express\lib\router\route.js:114:3)
        at Layer.handle [as handle_request] (D:\Node_JS\node_modules\express\lib\router\layer.js:95:5)
        at D:\Node_JS\node_modules\express\lib\router\index.js:284:15
        at param (D:\Node_JS\node_modules\express\lib\router\index.js:365:14)
        at param (D:\Node_JS\node_modules\express\lib\router\index.js:376:14)
        at Function.process_params (D:\Node_JS\node_modules\express\lib\router\index.js:421:3)
    Node.js v18.18.0
    [nodemon] app crashed - waiting for file changes before starting...
    
    뜨면서 서버가 죽는거 같습니다.
    
    { _id: new ObjectId(요청.params.id) }
    해당 라인이 문제가 있어보이는데 어떤게 문제일까요??
    
    (server.js) 
    app.get("/detail/:id", async (요청, 응답) => {
      console.log(요청.params.id);
      console.log(new ObjectId(요청.params.id));
      let result = await db
        .collection("post")
        .findOne({ _id: new ObjectId(요청.params.id) });
      응답.render("detail.ejs", { result: result });
    });
    
    (list.html)
    
    <%for(let i = 0; i < posts.length; i++) { %>
          <div class="list-box">
            <h4><%= posts[i]._id %></h4>
            <h4>"><%= posts[i].title %></h4>
            <p><%= posts[i].content %></p>
          </div>
          <%} %>
    
    서버를 실행하고 리스트에 있는 글 목록을 눌러서 처음에는 상세페이지에 들어가지는데 이후에 다른 글 목록의 상세페이지에 들어갈려고 하면 페이지가 나오지 않습니다
    그리고 처음에 상세페이지 들어가질때도 좀 많이 느리게 들어가집니다.
    #99614

    채성수
    참가자
    문제를 해결하려고 몇가지 확인을 해봤는데
    app.get("/detail/:id", async (요청, 응답) => {
      console.log(요청.params.id.length);
      console.log(요청.params);
      let result = await db
        .collection("post")
        .findOne({ _id: new ObjectId(요청.params.id) });
      응답.render("detail.ejs", { result: result });
    });
    get요청을 보낼시
    24
    { id: '65180ce32feebe666c96bd16' }
    8
    { id: 'main.css' }
    이렇게 두번의 결과가 나오고 
    어디서 main.css가 나왔는지 잘 모르겠습니다.
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <link rel="stylesheet" href="main.css" />
      </head>
      <body class="grey-bg">
        <%- include('nav.ejs')%>
        <div class="detail-bg">
          <h4>글제목임</h4>
          <p>글내용임</p>
        </div>
      </body>
    </html>
    detail.ejs 파일입니다.
    #99643

    codingapple
    키 마스터
    css파일 첨부경로를 /main.css로 바꿔봅시다
    #99683

    채성수
    참가자
    바꿨습니다! 혹시 어떠한 이유때문에 get요청시 main.css가 서버로 전송되는지 알 수 있을까요??
    #99705

    codingapple
    키 마스터
    원래 css파일 이미지 파일도 서버로 get요청해서 가져옵니다
    #112913

    이아인
    참가자
    같은 문제로 고생하다가 이 글 덕에 해결했습니다. 감사해요!
    #133894

    신재헌
    참가자
    이 글 덕분에 드디어 문제 해결 했습니다...ㅠ
7 글 보임 - 1 에서 7 까지 (총 7 중에서)
  • 답변은 로그인 후 가능합니다.

About

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

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

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