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

home2 게시판 Node.js, Express 게시판 없는 게시물 처리 (강의 : 쇼핑몰처럼 상세 페이지 만들기)

없는 게시물 처리 (강의 : 쇼핑몰처럼 상세 페이지 만들기)

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

    이해욱
    참가자
    app.get('/detail/:id', function(요청, 응답){
      var id= parseInt(요청.params.id);
      var sql = 'SELECT id, title, content FROM post WHERE id=?';
      connection.query(sql,[id],function(err,topics){
        console.log(topics);
        if (topics === null)
        {
          응답.status(500).send('Internal Server Error');
        }
        else
        {
          응답.render('index.ejs');
        }
      })
    })
    
    
    
    
    혹은
    
    
     
    app.get('/detail/:id', function(요청, 응답){
      var id= parseInt(요청.params.id);
      var sql = 'SELECT id, title, content FROM post WHERE id=?';
      connection.query(sql,[id],function(err,topics){
        console.log(topics);
        if(err)
                {
                    console.log(err);
                    응답.status(500).send('Internal Server Error');
                }
                else
                {
                    응답.render('index.ejs');
                }
    
    이렇게 해도안되네요
    #60156

    codingapple
    키 마스터
    app.get('/detail/:id 이게 하나 더 있는게 아닐까요 
    index.ejs보내라고 코드짰는데 detail.ejs가 보이는듯요
    #60168

    이해욱
    참가자
    app.delete('/delete', function(req, res){
        var id = req.body.id;
        var sql = 'DELETE FROM post WHERE id=?';
        connection.query(sql,[id],function(err,topics){
            if(err)
                {
                    console.log(err)
                    res.status(500).send('Internal Server Error');
                }
                else
                {
                    res.status(200).send({message: '성공했습니다.'});
                }
        });
    });
    app.get('/detail/:id', function(요청, 응답){
      var id= parseInt(요청.params.id);
      var sql = 'SELECT id, title, content FROM post WHERE id=?';
      connection.query(sql,[id],function(err,topics){
        if (topics === null)
        {
          응답.status(500).send('Internal Server Error');
        }
        else
        {
          응답.render('detail.ejs', {posts: topics[0]});
        }
      })
    })
    
    
    app.get('/detail/:id', function(요청, 응답){
      var id= parseInt(요청.params.id);
      var sql = 'SELECT id, title, content FROM post WHERE id=?';
      connection.query(sql,[id],function(err,topics){
        if (topics === null)
        {
          응답.status(500).send('Internal Server Error');
        }
        else
        {
          응답.render('detail.ejs', {posts: topics[0]});
        }
      })
    })
    server.js 이거 하나밖에 없습니다
    위에 오류가 뜬거는 id를 제가 직접 접속해본거입니다
    
    
    
    글 id list
    
    글 id 잇는거 접속
    
    
    id 없는거 접
    #60219

    codingapple
    키 마스터
    데이터가 없으면 topics 가 어떻게 나오는지 출력부터 해본 후에 if문을 씁시다
    #60225

    이해욱
    참가자
    [nodemon] restarting due to changes...
    [nodemon] starting `node server.js`
    listening on 80
    연결된듯
    []
    TypeError: C:\Users\atolo\Desktop\node\views\detail.ejs:22
        20|         <div class="center-menu">
        21|             <h4>상세페이지</h4>
     >> 22|                 <h4>제목: <%= posts.title %></h4>
        23|                 <p>내용 : <%= posts.content %></p>
        24|
        25|         </div>
    Cannot read properties of undefined (reading 'title')
        at eval ("C:\\Users\\atolo\\Desktop\\node\\views\\detail.ejs":15:32)
        at detail (C:\Users\atolo\Desktop\node\node_modules\ejs\lib\ejs.js:703:17)
        at tryHandleCache (C:\Users\atolo\Desktop\node\node_modules\ejs\lib\ejs.js:274:36)
        at View.exports.renderFile [as engine] (C:\Users\atolo\Desktop\node\node_modules\ejs\lib\ejs.js:491:10)
        at View.render (C:\Users\atolo\Desktop\node\node_modules\express\lib\view.js:135:8)
        at tryRender (C:\Users\atolo\Desktop\node\node_modules\express\lib\application.js:657:10)
        at Function.render (C:\Users\atolo\Desktop\node\node_modules\express\lib\application.js:609:3)
        at ServerResponse.render (C:\Users\atolo\Desktop\node\node_modules\express\lib\response.js:1039:7)
        at Query.onResult (C:\Users\atolo\Desktop\node\server.js:120:10)
        at C:\Users\atolo\Desktop\node\node_modules\mysql2\lib\commands\query.js:86:16
    
    topics가 null값이 뜨길래
    
    
    app.get('/detail/:id', function(요청, 응답){
      var id= parseInt(요청.params.id);
      var sql = 'SELECT id, title, content FROM post WHERE id=?';
      connection.query(sql,[id],function(err,topics){
        console.log(topics);
        if (topics == null)
        {
          응답.render('err.ejs');
        }
        else
        {
          응답.render('detail.ejs', {posts: topics[0]});
        }
      })
    })
    
    null값이 뜨면 err.ejs 로 render 해줘야되는데 오류가 뜨네요 ㅠㅠ
    #60270

    codingapple
    키 마스터
    topics가 null이 아니라 []로 뜨는데 if (topics.length == 0) 합시다
6 글 보임 - 1 에서 6 까지 (총 6 중에서)
  • 답변은 로그인 후 가능합니다.

About

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

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

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