What steps should I take to make my Vue JS delete function operational?

As I work on developing my website, I've encountered some challenges. Being new to coding, I'm struggling with creating a functional delete user button. When I click delete, it redirects me to the delete URL but doesn't remove the entry from my home table or MySQL database. Any advice would be greatly appreciated. Below is the code related to the issue:

Home Page Table;

<template>
  <div class="between:flex bottom:margin-3">
    <div class="center: flex-items">

      <!-- This section contains the template for showing entries in a table -->
      
    </div>
  </div>
  <div id="tableHolderDiv">
    <table class="table table-striped">
    
      <!-- Table headings -->

      <thead>
        <tr>
          <th scope="col">ID</th>
          <th scope="col">Location</th>
          <th scope="col">End User</th>
          <th scope="col">Order Number</th>
          <th scope="col">Date</th>
          <th scope="col">Application</th>
          <th scope="col">Service Tech</th>
          <th scope="col">Department</th>
          <th scope="col">Hours</th>
          <th scope="col">Travel Hours</th>
          <th scope="col">Contact Name</th>
          <th scope="col">Reason</th>
        </tr>
      </thead>
      <tbody>

        <!-- Displaying rows of data and each row has a delete button -->

        <tr v-for="row in serviceEntries" :key="row.id" @click="alertID(row.id)">
          <td scope="row">{{ row.id }}</td>
          <td>{{ row.location }}</td>
          <td>{{ row.end_user }}</td>
          <td>{{ row.order_number }}</td>

          <!-- Other table cell values -->

          <a href="/delete/{{this.id}}" type="button" class="btn btn-light btn-small" onclick="event.stopPropagation(); return confirm('Are you sure you want to delete this entry?');">
            <i class="bi bi-trash"></i> Delete
          </a>
          
        </tr>
        
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  name: "ServiceTable",
  computed: {
    serviceEntries() {
      return this.$store.state.serviceEntries;
    },
  },
  methods: {
  
    // Method to handle click on a row
    
    alertID(id) {
      this.$router.push({
        path: `/modify/${id}`,
      });
    },
  },
};
</script> 

The only Delete Call; in my app.js

app.delete("/api/service/:id", (req, res) => {
  pool.query(
    "DELETE FROM `master` WHERE id = ?",
    [req.params.id],
    function (error, results, fields) {
      if (error) res.json(error);
      else res.json(results);
    }
  );
});

Do I possibly need to add an app.post for delete?

Answer №1

GET method is being used by the a tag but you require a DELETE request. To achieve this, use a button along with a handler where you can utilize a library like axios or got for making various types of requests such as GET, POST, PUT, and DELETE.

<button
  class="btn btn-light btn-small"
  onclick="onDelete">
  <i class="bi bi-trash"></i>
  Delete
</button>
<script>
import axios from 'axios';

export default {
  methods: {
    async onDelete() {
      if(!confirm('Are you sure you want to delete this entry?') {
        return
      }
      await axios.delete(`/delete/${this.id}`);
    },
    alertID(id) {
      this.$router.push({
        path: `/modify/${id}`,
      });
    },
  },
};
</script> 

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

HTML - PHP, navigating the history of the page with the browser's back button in real-time

My current project involves the development of a website using HTML and PHP. On one of my HTML pages, I have a form with certain fields marked as "REQUIRED". To ensure that all required fields are filled before submission, I have implemented a feature wher ...

I'm having trouble with my fullscreen menu. Is there something I missed when setting it up?

Hi there! I'm currently working on my website and I seem to have run into an issue with the code. I'm still new to coding, so I would appreciate some assistance in identifying where I went wrong. I've been going through the code diligently t ...

Animate the background of a table cell (td) in React whenever a prop is updated

I have a dynamic table that receives data from props. I would like the background animation of each cell (td) to change every time it receives new props. I've tried creating an animation for the background to indicate when a cell is updated, but it on ...

How to retrieve the third party child component within a Vue parent component

Within my example-component, I have integrated a third-party media upload child component called media-uploader: <example-component> <form :action= "something"> // Other input types <media-upload :ref="'cover_up ...

Retrieve information filtered based on the query parameter

Utilizing react hooks for dynamic data rendering, I am focusing on two main tasks: a. Extracting URL parameters from the component's history props. b. Retrieving state data from the component's history props, which provides an array of objects ...

Using router-links to wrap cards for seamless navigation to different views

How can I wrap each workout card in a router-link inside this component to change to another view? Below is the code for reference: <template> <div class="card"> <img class="card-img-top" :src="info ...

Having trouble with getting the modal transition to work when closing it

Upon opening the modal, it transitions smoothly. However, when attempting to hide the modal, it does not transition out as expected - instead, it simply disappears. I previously included the within the modal (which worked in Vue2), but it appears to be c ...

Store the value returned from either the URI or the response in the test context using Cypress IO

I am struggling to figure out how to extract a specific portion of a key from both the URL and the xhr response. I initially attempted using the URI method but couldn't specify to save only part of the value. .url().then(($url) => { co ...

Javascript enables dynamic field addition to tables

I have JSON data that needs to be displayed in an HTML table. To input the values, I have individual fields for firstname, lastname, email, and phone number, along with an "Add Row" button. When I click the "Add Row" button, I want the entered values to b ...

Guide to expanding the sidebar width

As I delve into the world of CSS, I find myself puzzled by the issue of expanding the sidebar to ensure that the "Gestion des utilisateurs" section appears on a single line. https://i.stack.imgur.com/xzFEA.png To provide context, I have included an illus ...

What is the primary function for Express.js and Node.js 10 in Google Cloud Functions?

My Node JS version is 10 Express JS version: 4.16.1 I understand that for Node JS 8, the entry point should be "app" The generated code (from `npm install -g express-generator) www #!/usr/bin/env node /** * Module dependencies. */ var app = require( ...

The scrolling action triggered by el.scrollIntoViewIfNeeded() goes way past the top boundary

el.scrollIntoViewIfNeeded() function is used to scroll to element el if it's not within the visible browser area. Although it generally works well, I have encountered some issues when trying to use it with a fixed header. I have provided an example s ...

Encountering issues with JSON.Parse in JavaScript leads to errors

I'm encountering issues with JSON parsing in my code and I can't figure out the cause of it. I have a function that calls two ajax functions, one at the start and another in the success function. Everything seems to be working fine until I try to ...

Can I use npm's jQuery in an old-school HTML format?

I am looking to incorporate jQuery into a project without having to rely on the website or CDN for downloading the library. As someone new to npm, I am curious to know if following these steps would be advisable or potentially problematic down the line. Wh ...

improve your AJAX implementation in Django

Recently, I implemented some AJAX functionality in a Django application that I have been developing. Coming from a background in Ruby on Rails, I haven't had much experience with pure JavaScript. So, inspired by Rails' partials, I came up with ...

Vue.js 2.0: Resolving the Issue with "admissions is not defined"

I encountered a problem with my vue.js 2.0 code. Here is my HTML snippet: <div id="app"> <select class="form-control input-lg" name="admission_id" > <option>Select Admission type</option> ...

Can Authorization be Combined with Filtering in a Node.js RESTful API?

In my current setup, I have a web application communicating with a RESTful API to interact with the database. The frontend of the web app uses Angular HTTP to post/get/put data to the backend, which then manages authentication, interacts with the API, and ...

Creating Comet applications without the need for IFrames

Currently embarking on my journey to develop an AJAX application with server side push. My choice of tools includes Grizzly Comet on Glassfish V2. While exploring sample applications, I've noticed that most utilize IFrames for content updates on the c ...

Angular sends a POST request using $http to open in a new tab

In my Angular application, I am trying to send a POST request to a URL ./makeFile.php. This request will create a file with contents retrieved from a database query based on the data provided in the POST. When using PHP, the browser automatically opens a ...

JavaScript: Share module contents internally through exporting

When working with Java, there are 4 different visibility levels to consider. In addition to the commonly known public and private levels, there is also the protected level and what is referred to as the "default" or "package-local" level. Modifier Clas ...