What is preventing the DELETE and PUT methods from functioning on my website?

I am in the process of creating a simple website that functions as a task management application. Each HTTP method has one endpoint, and the methods being used are GET, POST, PUT, and DELETE. These methods need to be linked to functionalities such as adding, editing, and deleting tasks, setting due dates and reminders, and organizing tasks into categories or projects. However, I am encountering an issue where my delete and edit buttons are not functioning correctly. I will provide the HTML, Client-side JavaScript, and Server-side JavaScript code below.

<!DOCTYPE html>
<html>
  <head>
    <title>Task.me</title>
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <header>
      <img src="logo.png" alt="Task.me Logo" id="logo">
      <h1>Task.me</h1>
    </header>
    <main>
      <form id="add-task-form">
        <h2>Add Task</h2>
        <div class="form-group">
          <label for="new-task">Task:</label>
          <input type="text" id="new-task">
        </div>
        <div class="form-group">
          <label for="due-date">Due Date:</label>
          <input type="date" id="due-date">
        </div>
        <div class="form-group">
          <label for="category">Category:</label>
          <select id="category">
            <option value="">--Select a category--</option>
            <option value="Personal">Personal</option>
            <option value="Work">Work</option>
            <option value="School">School</option>
          </select>
        </div>
        <button>Add</button>
      </form>
      <section id="tasks-section">
        <h2>Tasks</h2>
        <ul id="task-list">
          <!-- List of tasks will go here -->
        </ul>
      </section>
    </main>

    <!-- Template for displaying tasks -->
    <template id="task-template">
      <li class="task-item">
        <form class="edit-task-form" hidden>
          <div class="form-group">
            <label>Task:</label>
            <input type="text" class="edit-task-name">
          </div>
          <div class="form-group">
            <label>Due Date:</label>
            <input type="date" class="edit-due-date">
          </div>
          <div class="form-group">
            <label>Category:</label>
            <select class="edit-category">
              <option value="">--Select a category--</option>
              <option value="Personal">Personal</option>
              <option value="Work">Work</option>
              <option value="School">School</option>
            </select>
          </div>
          <button>Save</button>
          <button type="button" class="cancel-button">Cancel</button>
        </form>
        <div class="view-task">
          <span class="task-name"></span>
          <span class="due-date"></span>
          <span class="category"></span>
          <button class="edit-button">Edit</button>
          <button class="delete-button">Delete</button>
        </div>
      </li>
    </template>

    <!-- Your HTML code goes here -->
    <script src="script.js"></script>

  </body>

</html>

JS CLIENT SIDE

// JavaScript code on the client side

// Function for retrieving tasks from the web service
async function getTasks() {
    const response = await fetch("/tasks");
    const tasks = await response.json();
    return tasks;
}
// Remaining code for handling task operations and display on the HTML follows below

SERVER SIDE

// server/index.js
// JavaScript code on the server side

// Required modules and initial setup for the server-side code
// Remaining code for handling server-side operations follows below

The errors I am encountering when clicking the DELETE and EDIT TASK buttons are displayed in the browser's developer tools:

EDIT BUTTON CLICKED: 
Task ID:NaN
Task element: -null

DELETE BUTTON CLICKED:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
RESPONSE: 
{
    "code": "ER_BAD_FIELD_ERROR",
    "errno": 1054,
    "sqlMessage": "Unknown column 'NaN' in 'where clause'",
    "sqlState": "42S22",
    "index": 0,
    "sql": "DELETE FROM tasks WHERE id = NaN"
}

Furthermore, I am utilizing a MySQL connection for the database. DATABASE SETUP

I am having trouble identifying the root of the issue. Any assistance provided would be greatly appreciated. Thank you.

Answer №1

Revise the handleEditButtonClick function in the JavaScript file to update the task.

const taskId = parseInt(event.currentTarget.dataset.taskId);

Similarly, update the handleDeleteButtonClick function in the JavaScript file to modify the task.

const taskId = parseInt(event.currentTarget.dataset.taskId);

Ensure you have a primary key set up as well.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

An empty array is being returned from a mongoose function call

I'm currently working on a project that involves fetching random values from MongoDB using mongoose and storing them in an array. However, I am facing an issue where the array appears to be empty outside the function: exports.Run = (req, res) => { ...

Is it possible to retrieve the pixel color of a mesh by clicking on it in JavaScript?

On the current page, we have a mesh loaded with TreeJS and displayed in a canvas: https://i.sstatic.net/j8Ztj.png Is there a way to retrieve the color of the point where a click occurs? I attempted a method suggested in this thread: Getting the color val ...

JSON retrieval line

Hi there, this is my first time posting here so I hope my question is clear :-) I have a PHP page that retrieves JSON data from a remote website. This JSON includes a list of names, and for each name, I need to fetch additional details from another JSON p ...

Unspecified data output from jquery datepicker

I am currently utilizing a datepicker to select a date. I have implemented the jQuery datepicker, however, it is returning an undefined value. Can someone please assist me with this issue? My HTML CODE: <input type="text" class="form-control fromdate" ...

When using PhpMailer to send emails, the response is not being returned to Javascript

Utilizing PhpMailer in a php/Javascript setup to send emails and while it is functional, it is not providing the expected success: function(response). Here is my javascript: <script type="text/javascript" language="javascript"> $ ...

Querying a collection with a bulk request using Mongoose Cursor

My current challenge involves working with rxJS and handling bulk HTTP requests from a database containing over 1 million documents. The code I have is relatively simple. I am pushing all the documents from the collection into an array called "allPlayers" ...

Assign a temporary value to the Select component in Material UI version 1.0.0-beta.24

I am currently working on a test project using Material UI v1.0.0-beta.24, and I have noticed that the "Dropdown" menus behave differently compared to the previous version. What I am trying to achieve is setting up a placeholder in the Select component. P ...

Showing the loading screen while waiting for the static Next.js app to load

Looking for a way to implement a loading screen right before the entire static page finishes loading? I'm currently utilizing modules:export to create my static page, but struggling to listen to the window load event since NextJs has already loaded th ...

Can you explain the significance of the colon in this context?

Upon reviewing some SearchKit code snippets (composed with react/jsx and es2015), I came across the following line in a jsx file: const source:any = _.extend({}, result._source, result.highlight) I am curious about the purpose or significance of the colo ...

Obtain the CSRF token from a webpage using JavaScript

I am currently working on a project where I need to retrieve the csrf token from a web page built with Django using javascript. The structure of my HTML template is as follows: <div class = "debugging"> <p id = "csrf">{% csrf_token %}</p ...

Ways to avoid scrolling on a fixed element

Here is the HTML setup I have... body .top .content The issue I am facing is that when scrolling reaches the end of the ul in the .top element, the background starts to scroll. This can be quite disorienting and makes the site slow on tablets. Even ...

Leverage the camera functionality in both native and web applications using Ionic/AngularJS and Cordova

Could you provide some guidance on how to use the Camera feature in both web and native environments? I have tried implementing it using the code snippet below, taken from ng-cordova documentation: $scope.takePicture = function() { var options ...

Adjust the template once it has been compiled

I have a basic design that makes use of ng-repeat: design.html <div id='design'> <ul> <li ng-repeat="item in items">{{ item.desc }}</li> </ul> </div> Within my guidance, I am combining that design wi ...

Effortlessly update by clicking the new button on Kendo Grid

I am currently using the Kendo UI grid to connect to a MySQL database. While the update and read functions are working fine, I am facing an issue with the create function. Whenever I input data into a new row and click update, nothing happens. I am unsure ...

`Node.js: Implementing intricate routes with Express`

Just starting out with Node.js and Express here. I have a question about setting up routes in Express.js. In my project, I need to match the following URL: example.com/?campaign=123&click=123. I've tried using the following condition in the app ...

What exactly is an instance in React?

class WOW extends React.Component { render() { return <h1>WOWZA!</h1>; } } class Zany extends React.Component { render() { return <WOW />; } } within the above code snippet, the Crazy class's render method returns a ...

Issue with BrowserRouter, improperly looping through the array using map

Encountering an issue with importing content in my React app project using <BrowserRouter>. Within the app, there are 3 Material-UI Tabs: /lights, /animations, /settings. <Switch> <Route path="/lights" component={LightsMenu} /> ...

forever js is interfering with the operation of a different application

I currently have two very similar Node JS projects that I manage by starting and stopping them using Forever JS. Both projects can run simultaneously on different ports but, when I input the following command: forever stop index.js In one project direc ...

Create a custom JavaScript library by incorporating an external library into it as a bundle

As I work on developing a library, one of the dependencies I plan to use is fabricjs. Installing fabricjs involves specific libraries and versions that can be cumbersome for users. Despite successfully installing it in my project and using it, my concern l ...

What is the cost associated with using the require() function in an Express.js application?

I have a web application built with Express.js that serves one of my domains. The structure of the app.js file is as follows: var express = require('express'); var app = express(); // and so on… To incorporate one of my custom functions in t ...