Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with deleting in Node.js, Mongodb, Ajax, HTML

I'm having trouble getting my delete button to work. I have a table with 4 row also a delete button. I have written this code and would like to delete a task, through fileName or _id.

Code in Server.js

app.delete("/deleteTask/:id", function (req, res) {
console.log("TEST");
const task_id = req.params.id;
taskCollection.deleteOne({ "_id" : ObjectId(task_id) }, function (err, success) {
    console.log(success);
    res.redirect("/");
});
res.send(); });

Code in View.js

$.ajax({
    "url": "/featured-tasks",
    "method": "GET"
}).done(function(tasks) {
    tasks.forEach(function(task) {
        const taskCard =   "<tr class='task-card'>" +
                                 "<td class='title'>" + task.title +  "</td>" + "<td class='description'>" + task.description + "</td>" +
                                 "<td class='courses'>" + task.courses + "</td>" + 
                                 "<td class='task'>" + "<a href='/view?task=" + task.fileName + "'>" + task.fileName +"</a>" + "</td>" +
                                 "<td>" + "<button id='taskdelete' class='btn-delete' data-target='/deleteTask/' data-method='DELETE' data-disabled='true'>" + "Delete" +"</button>" + "</td>" +
                                 "</tr>";
        $(".task-gallery").append(taskCard);
    })
});


$('#taskdelete').on('click', function() {
    const taskFileName = $(this).attr('task.fileName');
    console.log("test");
    $.ajax({
       method: "DELETE",
       url: "/deleteTask/" + taskFileName,
       success: function(result) { {
              location.reload();
          }
       }
    })
 });

HTML fragment for table:

<table class="task-gallery">
<tbody>
<tr class="tableHeader">
<th>Title</th>
<th>Description</th>
<th>Courses</th>
<th>PDF-file</th>
<th></th>
</tr>
<script src="home/home.js"></script>
<tr class="task-card">
<td class="title">Larman</td>
<td class="description">lektier</td>
<td class="courses">info</td>
<td class="task"><a href="/view?task=file.pdf">file.pdf</a></td>
<td><button class="btn-delete" data-filename="file.pdf" data-method="DELETE" data-disabled="true">Delete</button></td>
</tr>
</tbody>
</table>

1 Answers

There are a few things to change in your frontend code:

  • Id values for HTML elements need to be unique so you can't use your click handler
  • You need to set the task.fileName value as an attribute of the delete button.
  • The onclick event handler should be on the container element.

Here is some updated code for you to try:

$.ajax({
  "url": "/featured-tasks",
  "method": "GET"
}).done(function(tasks) {
  tasks.forEach(function(task) {
      const taskCard = "<tr class='task-card'>" +
        "<td class='title'>" + task.title +  "</td>" + "<td class='description'>" + task.description + "</td>" +
        "<td class='courses'>" + task.courses + "</td>" + 
        "<td class='task'>" + "<a href='/view?task=" + task.fileName + "'>" + task.fileName +"</a>" + "</td>" +
        "<td>" + "<button class='btn-delete' data-filename='" + task.fileName + "' data-method='DELETE' data-disabled='true'>" + "Delete" +"</button>" + "</td>" +
        "</tr>";
      $(".task-gallery").append(taskCard);
    })
});


$('.task-gallery .btn-delete').on('click', function() {
  const taskFileName = $(this).attr('data-filename');
  console.log("test");
  $.ajax({
    method: "DELETE",
    url: "/deleteTask/" + taskFileName,
    success: function(result) {
      location.reload();
    }
  })
});

On the server side you should check for an error:

app.delete("/deleteTask/:id", function (req, res) {
  console.log("TEST");
  const task_id = req.params.id;
  taskCollection.deleteOne({ _id : ObjectId(task_id) }, function (err, success) {
    if (err){
      console.log("failed");
      throw err;
    }
    console.log(success);
    res.redirect("/");
  });
  res.send();
});

The code I have provided is correct, run the code snippet to verify it for yourself:

var tasks = [
  {
    title: "A Course",
    description: "Description of A",
    courses: "Course A",
    fileName: "file-A.pdf"
  },
  {
    title: "B Course",
    description: "Description of B",
    courses: "Course B",
    fileName: "file-B.pdf"
  },
  {
    title: "C Course",
    description: "Description of C",
    courses: "Course C",
    fileName: "file-C.pdf"
  }
];

tasks.forEach(function(task) {
  const taskCard = "<tr class='task-card'>" +
  "<td class='title'>" + task.title +  "</td>" + "<td class='description'>" + task.description + "</td>" +
  "<td class='courses'>" + task.courses + "</td>" + 
  "<td class='task'>" + "<a href='/view?task=" + task.fileName + "'>" + task.fileName +"</a>" + "</td>" +
  "<td>" + "<button class='btn-delete' data-filename='" + task.fileName + "' data-method='DELETE' data-disabled='true'>" + "Delete" +"</button>" + "</td>" +
  "</tr>";
  $(".task-gallery").append(taskCard);
});

$('.task-gallery .btn-delete').on('click', function() {
  const taskFileName = $(this).attr('data-filename');
  // spit out the 'data-filename' value
  window.alert('Delete ' + taskFileName);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<table class="task-gallery">
  <tbody>
    <tr class="tableHeader">
      <th>Title</th>
      <th>Description</th>
      <th>Courses</th>
      <th>PDF-file</th>
      <th>&nbsp;</th>
    </tr>
  </tbody>
</table>
like image 137
Dan Nagle Avatar answered Apr 17 '26 01:04

Dan Nagle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!