-
글쓴이글
-
2022년 4월 14일 10:42 #31735
냠냠냠참가자안녕하세요... 글 수정 페이지를 만들고 있는데 도움을 받고 싶어 글 올립니다.
to do list 에서 수정 버튼 누르면 수정 페이지로 넘어가고, 수정 완료 후 수정 버튼 누르면 다시 to do list 로 돌아가는 형식으로 만들었습니다.
그런데 to do list 에서 수정 버튼 눌렀을 때 요청.body 에 아무것도 넘어가지 않습니다.
그리고 수정페이지에서 수정 버튼 눌렀을 때는 Cannot GET /edit 이라는 오류가 나옵니다.
to do list 에서는
<div class="container">
<ul class="list-group">
<% for (var i = 0; i < myposts.length; i++){ %>
<li class="list-group-item">
<h4> 할 일 : <%= myposts[i].제목 %> </h4><% if ( myposts[i].완료 == true) { %>
<p> 완료 </p>
<% } %>
<% if ( myposts[i].완료 == false) { %>
<p> 진행 중 </p>
<% } %>
<!-- <p> 공개 : <%= myposts[i].공개 %> </p> -->
<p> 날짜 : <%= myposts[i].날짜 %> </p>
<p> 우선순위 : <%= myposts[i].우선순위 %> </p>
<p> 메모 : <%= myposts[i].메모 %> </p>
<button class="btn btn-outline-primary delete" data-id="<%= myposts[i]._id %>">삭제</button>
<!-- <button class="btn btn-outline-primary edit" data-id="<%= myposts[i]._id %>"">수정</button> -->
<button class="btn btn-outline-primary edit" onclick="location.href='/edit/<%= myposts[i]._id %>'"data-id="<%= myposts[i]._id %>">수정하기</a></button>
<% if ( myposts[i].완료 == false) { %>
<button class="btn btn-outline-primary complete" onclick="location.href='/complete'" data-id="<%= myposts[i]._id %>">할 일 완료</button>
<% } %>
</li>
<% } %>
</ul>
</div>이런 식으로 수정하기 버튼 누르면 각 포스트의 글번호가 넘어가게 넣어놨는데 왜 요청.body 에 아무것도 나오지 않을까요??
server.js 에 글수정부분은
//수정 버튼 눌렀을 때 글 수정
app.put('/edit', function(요청, 응답){
console.log('글수정데이터전송', 요청.body)
요청.body._id = parseInt(요청.body._id); //'1'이라는 문자를 정수로 변환
//요청.body에 담긴 게시물 번호에 따라 DB에서 게시물 삭제
console.log('수정버튼눌렀을때 그냥 edit')
console.log(요청.body._id)
console.log(요청.user.id)var 수정할데이터 = {_id : 요청.body._id, 작성자 : 요청.user.id}
db.collection('post').updateOne(수정할데이터,{ $set : {제목 : 요청.body.title, 날짜 : 요청.body.date, 우선순위 : parseInt(요청.body.priority), 메모 : 요청.body.memo} },function(에러, 결과){
if(에러){return console.log(에러)};
console.log('수정 완료');
console.log(결과);
console.log(요청.body.title)
응답.send("<script>alert('수정 완료되었습니다.'); window.location.replace('/mytodo');</script>");
})
});//글 수정 페이지에 데이터 뿌리기
app.get('/edit/:id', function(요청, 응답) {
console.log('글수정데이터뿌리기', 요청.body)
//요청.body._id = parseInt(요청.body._id); //'1'이라는 문자를 정수로 변환
console.log('아이디', 요청.user.id)
//요청.body에 담긴 게시물 번호에 따라 DB에서 게시물 삭제
db.collection('post').findOne({작성자 : 요청.user.id},function(에러, 결과){
if(에러){return console.log(에러)};
console.log('데이터뿌리기')
console.log(결과);
응답.render('edit.ejs', {post : 결과});
});
});이렇게 작성했습니다.
그리고 글 수정 페이지는 form 태그로 작성해서 action에 /edit 으로 put 하는 방식으로 넘겼습니다.
<div class="container mt-3">
<form action="/edit" method="PUT">
<div class="form-group">
<label>할 일</label>
<input value="<%= post.제목 %>" type="text" class="form-control" name="title">
</div>
<div class="form-group">
<label>마감 날짜</label>
<input value="<%= post.날짜 %>" type="text" class="form-control" name="date">
</div>
<div class="form-group">
<label>우선 순위 (이전 선택 : <%= post.우선순위 %> )</label><br>
<div class="form-check form-check-inline" name="priority">
<input class="form-check-input" type="radio" name="priority" id="prior1" value="1">
<label class="form-check-label" for="inlineRadio1">매우 중요(1)</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="priority" id="prior2" value="2">
<label class="form-check-label" for="inlineRadio2">중요(2)</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="priority" id="prior3" value="3">
<label class="form-check-label" for="inlineRadio3">보통(3)</label>
</div>
</div>
<div class="form-group">
<label>메모</label>
<input value="<%= post.메모 %>" type="textarea" class="form-control" name="memo">
</div>
<button type="submit" class="btn btn-outline-secondary">수정</button>
</form>
</div>글 수정할 때 데이터 뿌리는 건 일단 임시방편으로 아이디로 db에서 찾아서 넣어놓긴 했는데 글 번호로 찾아서 넣고 싶어도 요청.body 가 { } 이라서 가져올 수가 없습니다....
콘솔에는 이런식으로 출력됩니다.
글수정데이터뿌리기 {}
아이디 love
데이터뿌리기
{
_id: 4,
'작성자': 'love',
'제목': 'test - love',
'날짜': '2022-04-01',
'메모': 'love 아이디',
'우선순위': '3',
'완료': true,
'공개': false
}제가 생각하기엔 todolist 에서 수정 버튼 누르면 -> 글번호 _id 와 함께 -> server.js에 app.get 으로 지정해놓은 edit/:id 로 넘어가고 // 글 수정 페이지에서 수정 버튼 누르면 // form 태그에 action 넣어놓은 /edit으로 put으로 form 태그 내용이 다 넘어가고 -> server.js에 app.put으로 지정해놓은 /edit 에 가게 되면서 마지막에 redirect 로 다시 todolist 로 넘어가는 그런 구조라고 생각했는데.... 어디서부터 잘못된 걸까요............
제가 어떻게 고쳐야할까요....???? 도움 부탁드립니다........
2022년 4월 14일 16:31 #31756
codingapple키 마스터<form action="/edit" method="PUT">
form태그에서 바로 put요청은 못날립니다 method override 이런거 쓰거나 get/post 쓰거나 둘 중 하나 하면 됩니다
2022년 4월 15일 11:17 #31800
냠냠냠참가자감사합니다! 말씀해주신대로 method는 POST로 수정했는데...
***************여전히 어떤 글의 수정 버튼을 눌렀을 때 해당 글의 글 번호가 넘어오지 않아서 원하는 글을 수정못 하고 있씁니다... 글 번호 가져오는 방법 중에 제가 틀린 것이 있을까요?*************
그리고 form 태그에 post로 수정하니, 글이 수정됐다는 alert까지는 나왔습니다. 그런데
콘솔에서는
//글수정페이지에 데이터 뿌리기
app.get('edit/:id') 이부분의 콘솔로그에서
요청.body 가 { } 이렇게 출력됩니다.
또 글번호도 요청.body._id 로 콘솔로그 뽑아보니 undefined 라고 나왔습니다.
//수정버튼 눌렀을 때 글 수정
app.post('/edit') 이부분을 put에서 post로 수정하였고,
여기서는 요청.body_id 가 제대로 출력되고 있습니다.
또, 콘솔로그에 요청.body.title 을 찍어보니 수정하며 입력한 데이터가 잘 나옵니다.
그리고 글 수정 데이터가 전송되고 있는데도 MongoDB 들어가면 데이터 수정은 안 되어있습니다..... 어떻게 해야할까요???
2022년 4월 15일 13:00 #31814
냠냠냠참가자수정페이지에서
<form action="/edit" method="POST">
<input type="hidden" name="_id" value="<%= post._id %>">이런식으로 hidden으로 글 번호를 넘기고, 글 수정 버튼에도
<button type="submit" class="btn btn-outline-secondary"data-id="<%= post._id %>">수정</button>
이렇게 넘겼습니다.
이걸로 server.js / app.post 부분에서 데이터를 쓸 수 있는 거라면
server.js / app.get 으로 받아오는 부분은
투두리스트에서 수정 버튼에
<button class="btn btn-outline-primary edit" onclick="location.href='/edit/<%= myposts[i]._id %>'"data-id="<%= myposts[i]._id %>">수정하기</a></button>
이렇게 글 번호 넘기는 부분이라고 생각했는데 왜 한쪽은 데이터가 나오고 한쪽은 { } 로 출력되는 걸까요? 콘솔로그에 글수정데이터뿌리기 {} 부분을 어떻게 해결할 수 있을지... 도움 부탁드립니다....
2022년 4월 15일 16:28 #31829
codingapple키 마스터app.get('edit/:id') 이런 get요청에선 body같은게 출력되지 않습니다 :id에 적은 글자같은 url 파라미터는 출력가능합니다
서버에 데이터가 잘 들어오는데 db에서 수정이 안된다면
_id가 틀렸거나 db에 요청하는 코드가 잘못되었거나 그런걸 살펴보면 됩니다
-
글쓴이글
- 답변은 로그인 후 가능합니다.