Project/축구모임 홈페이지개발

[축구모임 홈페이지개발] 12/23 개발일지 / 검색결과 탭 삭제기능, 삭제권한 기능 추가

한33 2023. 12. 24. 12:39

경기일정에서 특정 아이디로 접속시 경기일정을 삭제할 수 있는 기능을 넣고 싶었다.

 

        <% if (typeof 유저 !=='undefined' ) { %>
          <% if (유저=='test' ) { %>
            <div class="match-result-delete">
              <a href="#" data-last-result-id="<%=result[0]._id%>" onclick="confirmDeleteLastResult(event)">삭제</a>
            </div>
            <%}}%>

 

위와 같이 삭제버튼을 만들었고, 이름이 test 인 유저에게만 삭제가 보이게 만들었다.

그리고 위처럼 만들면 로그인이 안되어있는 상태에서는 분명 에러가 뜰 것이기 때문에 유저가 있을 때, 유저 이름이 test 면 보이게 했다.

 

    function confirmDeleteResult(event) {
      event.preventDefault();

      var commentId = event.target.getAttribute('data-result-id');
      var confirmDelete = confirm('글을 삭제하시겠습니까?');

      if (confirmDelete) {
        window.location.href = '/match-result-delete/' + commentId;
      }
    }

 

그리고 버튼이 눌리면 위와 같은 함수가 실행되도록 했다.

 

서버쪽에서 전달받은 요청을

 

app.get('/match-result-delete/:id', async (req, res) => {
  let result = await db.collection('result').deleteOne({
    _id: new ObjectId(req.params.id)
  })
  res.redirect('back')
})

 

위와같이 처리해주어서 삭제 기능을 구현했다.