Nodejs - error message, n-th failure outputs n number of failure messages

Viewed 16

I would like to ask you a question about the error message of the Node.Js.

Specifically, it is a question about the number of printed messages.

In my server.js, I made some situation for send status 400. Then list.ejs receives information about the server status, then print message list.ejs - fail for deleting in console.

But the number of messages printed is different for each failure. The first failure outputs one message and the second failure outputs two messages, n-th failure outputs n messages. ( You can watch below gif what happens in my code ).

Please tell me why it is like this.

I wrote the code as below.

  • server.js
app.delete('/delete', (req, res) => {
  // console.log(req.body);
  req.body._id = parseInt(req.body._id);
  var deleteObj = {_id: req.body._id, writer: req.user._id};
  // var deleteObj = {_id: req.body._id};
  db.collection('post').deleteOne(deleteObj, (errDeleteOne, resDeleteOne) => {
    if (errDeleteOne) {
      res.status(400).send({message: 'server.js - error in deleting'});
      return console.log(errDeleteOne);
    }
    if (resDeleteOne.deletedCount == 0) {
      res.status(400).send({message: 'server.js - not have permission to delete'});
      return console.log('server.js - console.log - not have permission to delete');
    }
    console.log('server.js - console.log - success for deleting');
    console.log(resDeleteOne);
    res.status(200).send({message: 'server.js - success for deleting'});
  });
});
  • list.ejs
    <script>
      $('.delete').click((event) => {
        var clickedId = event.target.dataset.id;
        var clickedItem = event.target;
        document.querySelector('.modal').style.display = 'block';

        $('.btn-close').click((event) => {
          document.querySelector('.modal').style.display = 'none';
        })
        
        $('.btn-confirm').click((event) => {
          $.ajax({
            method : 'DELETE',
            url: '/delete', // 요청할 경로
            data: {_id : clickedId} // 요청과 함께 보낼 데이터
          }).done((res) => {
            document.querySelector('.modal').style.display = 'none';
            console.log('list.ejs - success for deleting');
            $(clickedItem).parent('li').fadeOut();
          }).fail((xhr, code, err) => {
            document.querySelector('.modal').style.display = 'none';
            console.log('list.ejs - fail for deleting')
          })
        })
      })
    </script>

[implementation gif]

0 Answers
Related