-
글쓴이글
-
2021년 3월 14일 17:17 #7132
황유정참가자edit.ejs 파일을 view 폴더에 작성하고, 코드를 다음과 같이 작성했습니다.
<!--more-->
<div class="container mt-3">
<form action="/edit?_method=PUT" method="POST">
<div class="form-group">
<label>오늘의 할일</label>
<input type="text" value="<%= post._id %>" name="id" style="display: none;">
<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>
<button type="submit" class="btn btn-outline-secondary">Submit</button>
</form>
</div><!--more-->
문제는 2번째 줄 action 태그에서 발생하는 것 같은데요. 코드를 똑같이 구성했는데 저만 다르게 표시되는 이유를 모르겠습니다.
어떤 현상이냐면
localhost:8800/edit/6 로 접속하여
db로부터 데이터한개를 호출하는 것은 성공했는데, 데이터 수정 후 submit 버튼을 클릭하게 되면,
서버.js에 작성한 app.put 함수가 동작하지 않고 connot Post /edit 이라는 문구가 브라우저 화면에 띄워집니다.
서버.js에 작성한 데이터는
<!--more-->
app.get('/edit/:id', function(요청, 응답) {
db.collection('post').findOne({_id : parseInt(요청.params.id)}, function(에러, 결과) {
console.log(결과)
응답.render('edit.ejs', { post : 결과 });
})
})app.put('/edit', function(요청, 응답) {
// 폼에 담긴 제목, 날짜 데이터를 가지고 db.collection 에 업데이트 함
// updateOne(어떤게시물수정할건지, 수정값, 콜백함수)
// $set : 업데이트 해주세요 (없으면 추가해주시구요)
console.log(요청)
db.collection('post').updateOne({ _id : parseInt(요청.body.id) }, { $set : { 제목 : 요청.body.title, 날짜 : 요청.body.date }}, function(에러, 결과) {
console.log('수정완료');
응답.redirect('/list');
})
})<!--more-->
위 코드와 같이 작성하였습니다.
혹시나 하여 npm install mothod-override 를 재 설치후 진행했음에도 안되는 것 같습니다.
아마 주소값 때문인 것 같은데, 정확한 원인을 모르겠네요.
어떻게 넘어가냐면
localhost:8800/edit?_method=PUT
이라고 action 속성에 기입한 텍스트가 문자 그대로 전송됩니다.
어떻게 해결해야할까요?
2021년 3월 14일 19:19 #7133
codingapple키 마스터server.js 위에서 method-override 설치한거 가져오고 app.use()로 등록까지 하셨습니까
-
글쓴이글
- 답변은 로그인 후 가능합니다.