7 글 보임 - 1 에서 7 까지 (총 7 중에서)
-
글쓴이글
-
2023년 2월 7일 22:19 #67294
이수인참가자안녕하세요 nodejs 강의를 수강하고 있는 수강생입니다. 저번에 질문을 드렸었고 답글로 관련 질문을 남겼었습니다. 그런데, 댓글이 달리지 않아 알림이 안 갔나 생각해서 다시 질문도 정리하는 겸, 새로운 글 남깁니다. detail/9는 있지만 detail/90은 없어서 접속시에 에러를 띄우는 코드를 구현 중인데 계속 실패하고 원인을 잘 모르겠습니다. app.get('/detail/:id', function(요청, 응답){ db.collection('post').findOne( { _id : parseInt(요청.params.id) }, function(에러, 결과){ console.log(에러);//=>null if(에러){ 응답.status(500).send('500!!'); }else{ console.log(결과); 응답.render('detail.ejs', { data : 결과 }); } }) }) 위는 js 파일, <div class="container mt-3"> <h4 class = "text-center">Detail Page</h4> <div class="card" style="width: 100%"> <div class="card-body"> <h5 class="card-title"><%= data.제목 %></h5> <h6 class="card-subtitle mb-2 text-muted"><%= data.날짜 %></h6> <p class="card-text">임시내용입니다.</p> edit </div> </div> </div> 위는 detail.ejs입니다. 저는 js 파일에 if문에서 걸려서 500error를 띄우려 했습니다. 그런데 예상대로 실행되지 않고 웹페이지 상에서는
TypeError: /workspace/coding_apple/views/detail.ejs:22 20| <div class="card" style="width: 100%"> 21| <div class="card-body"> >> 22| <h5 class="card-title"><%= data.제목 %></h5> 23| <h6 class="card-subtitle mb-2 text-muted"><%= data.날짜 %></h6> 24| <p class="card-text">임시내용입니다.</p> 25| " class="card-link btn btn-danger">edit
Cannot read property '제목' of null at eval (eval at compile (/workspace/coding_apple/node_modules/ejs/lib/ejs.js:673:12), <anonymous>:15:31) at detail (/workspace/coding_apple/node_modules/ejs/lib/ejs.js:703:17) at tryHandleCache (/workspace/coding_apple/node_modules/ejs/lib/ejs.js:274:36) at View.exports.renderFile [as engine] (/workspace/coding_apple/node_modules/ejs/lib/ejs.js:491:10) at View.render (/workspace/coding_apple/node_modules/express/lib/view.js:135:8) at tryRender (/workspace/coding_apple/node_modules/express/lib/application.js:657:10) at Function.render (/workspace/coding_apple/node_modules/express/lib/application.js:609:3) at ServerResponse.render (/workspace/coding_apple/node_modules/express/lib/response.js:1039:7) at /workspace/coding_apple/server17.js:105:7 at executeCallback (/workspace/coding_apple/node_modules/mongodb/lib/operations/execute_operation.js:70:5)
위의 코드가 출력됩니다. 저번 질문에서 결과가 비어있는지 안 비어있는지로 체크한다고 하셨는데, 무슨 소리인지 이해가 되지 않았습니다. 구글링을 해서 // app.use(function(req, res, next) { // res.status(404).render('404 error page!'); // }) // app.use(function (err, req, res, next) { // console.error(err.stack) // res.status(500).send('500 error page!') // }); 위 코드를 작성하게 되면 정상적으로 에러처리가 됐지만, 위 코드를 작성하지 않고 js 파일에서 if문으로 에러를 처리하려면 어떻게 해야 하는지 알고 싶습니다. 또 function의 인자인 '에러'의 값이 null인데 왜 null인 건가요?
2023년 2월 8일 09:48 #67328
codingapple키 마스터db에서 찾은 내용이 없으면 에러 파라미터에 뭐가 들어오지않고 결과 파라미터만 비어있습니다 결과 출력해봅시다
2023년 2월 8일 23:09 #67493
이수인참가자처음 질문 때 결과가 비어있는지 안 비어있는지 체크하라는 게 if문에 결과가 null인지 확인해서 오류를 체크하라는 것이었군요!! 이것 때문에 며칠 고생했는데 코딩애플님 덕분에 시원하게 해결된 것 같습니다!! 그런데 db에 찾는 내용이 없을 때 왜 404 error가 아닌 500 error인지 궁금합니다. 자료가 없으니 404 error가 떠야되는 것이 아닌가요?
2023년 2월 9일 22:04 #67672
이수인참가자아 제가 질문을 자세히 쓰지 않은 것 같네요. if문을 쓰지 않고 응답.render('edit.ejs', {edition:결과}) 이렇게만 쓴 상태에서 없는 페이지에 접속시, console창에서 Failed to load resource: the server responded with a status of 500 (Internal Server Error) 이런 문구가 뜹니다. 그래서 처음 에러처리할 때 500error로 하려 했습니다. 이 상황에서 왜 500error 가 뜨는 건가요?
-
글쓴이글
7 글 보임 - 1 에서 7 까지 (총 7 중에서)
- 답변은 로그인 후 가능합니다.