Implementing the delete functionality in a mongoose model and displaying the result in an object page using mongo

I am currently facing an issue where I am unable to delete a specific 'ticket' from the database. The webpage shows a table listing all the tickets with their respective seat names and prices, along with a delete button (form). However, when I try to delete a ticket using the delete button, it always deletes the first ticket in the list, no matter which one I select.

Below is the controller function:

    Ticket.findOneAndDelete(req.params.id, function(err) {
      console.log('The delete button works in the Tickets router')
        res.redirect('/tickets');
      });
    };

Router:

router.post('/tickets/:id', ticketsCtrl.delete)

EJS snippet:

        <tr>
          <td><%= t.seat %></td>
          <td><%= t.price %></td>
          <td>
          <form id="delete-ticket-form" method="POST"
          action="/tickets/<%= t._id %>">
          <input type="submit" value="X">
        </td>
        </tr> 

Answer №1

My JavaScript code was spot on, but I accidentally forgot to close a tag properly. *facepalm

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

Challenges with inferring return values in Typescript generics

I'm encountering an issue with TypeScript that I'm not sure if it's a bug or an unsupported feature. Here is a Minimal Viable Example (MVE) of the problem: interface ColumnOptions<R> { valueFormatter(params: R): string; valueGette ...

Is the ng-if directive malfunctioning?

After calling console.log($scope.showAddEvent), it is evident that the variable showAddEvent is being updated. However, the ng-if directive does not seem to reflect these changes as it does not display anything at all. As I am relatively new to angularjs, ...

Retrieve the class name based on the ID of the final appearance of the div using jQuery

<div class='user user1' id='newuser'></div> <div class='user user2' id='newuser'></div> <div class='user user3' id='newuser'></div> To retrieve the class name ...

How can I retrieve data during a double-click event in Kendo Grid using Angular?

How can I retrieve data on the doubleClick event in a Kendo Grid? I want to access the same object that is fetched during the selected event, which would be the dataitem at the selected index row. HTML: <kendo-grid #myGrid [data]="gridDat ...

Error: The term "require" is not recognized in the context of the React

After creating my own React component as an NPM package and publishing it on NPM, I encountered an error when trying to import and use it in other Create React App (CRA) projects. The error occurs when running npm start in the command line. See the screens ...

Utilizing AJAX POST requests from JavaScript to a Rails 4 controller while implementing Strong Parameters

As a newcomer to Rails, I am looking to insert song_id and title received from JavaScript via AJAX POST into a MySQL database. In my JavaScript file: var song_id = "23f4"; var title = "test"; $( document ).ready( function() { jQuery.ajax({ ...

Implementing CORS with Express and React on Heroku

Encountering an Error: Access to XMLHttpRequest at 'http://cheapandnice-backend.herokuapp.com/api/products/list' from origin 'http://cheapandnice.herokuapp.com' has been blocked by CORS policy: No 'Access- Control-Allow-Origin&a ...

Why is there a presence of quotation marks in the value stored in my local storage?

I have been attempting to extract data from a MySQL database using the following code: app.get("/api/getStudentsFromClass", async(req,res) => { const currentClassClicked = req.query.currentClassClicked connection.query( " ...

Execute JavaScript function when the page is being refreshed or accessed from an external link

On the Home page, we have a special Lottie animation that serves as a preloader. This animation should only display under certain conditions: Accessing the home page by clicking on a link from an external page (not on your website) Refreshing the brow ...

Is there a way to pause the scrolling on the ScrollPath jQuery plugin when it reaches a specific element?

Is it possible to pause the jQuery ScrollPath plugin at each DIV for a brief period of time? I have observed similar functionality in other plugins and find it quite useful. I have come across this feature on various websites, where the scrolling stops mo ...

Why would one assign setTimeout to a variable and is it necessary to then clear it using clearTimeout?

I have been pondering the purpose of assigning setTimeout to a variable like this: scroll_timer = window.setTimeout(function () { ... when I could simply use: window.setTimeout(function () { ... Do you think there is actually a need to clearTimeout in ...

Troubleshooting the Connect Button in Chapter 3 of 'Mastering XMPP Development with JavaScript and jQuery'

Currently, I am in the process of setting up an xmpp client on my website and to familiarize myself, I am following the examples provided in a book. Example 3 has been successfully implemented by copying the code, with the only alterations being the stroph ...

Creating a one-of-a-kind entry by adding a number in JavaScript

I am looking for a way to automatically add an incrementing number to filenames in my database if the filename already exists. For example, if I try to add a file with the name DOC and it is already present as DOC-1, then the new filename should be DOC-2. ...

How can one elevate the properties of each item's sub-structure to the root level in an array containing object items?

I am working with an array containing objects, each with a specific structure... [{ user: { /* ... more (nested) user data ... */ }, vacation: { id: 'idValue', name: 'nameValue', startDate: 'dateValue', }, }, ...

How to pass children and additional arguments to a React/NextJS component

Currently, I am utilizing NextJS with a global PageLayout wrapper that is responsible for setting the head and creating the wrapping divs for all my pages. However, I am facing a challenge as I attempt to set a custom title tag for each page. This task req ...

Detecting hidden child divs due to overflow: hidden in Angular6

Below is the particular issue I am aiming to address. <div class="row" style="overflow:hidden;"> <app-car *ngFor="let car of cars; trackBy: trackByFunction" [car]="car" > </app-car> </div> <button> ...

The width of the Bootstrap row decreases with each subsequent row

I'm having trouble understanding this issue, as it seems like every time I try to align my rows in bootstrap, they keep getting smaller. Can anyone point out what mistake I might be making? ...

Characteristics within the primary template element of a directive

I'm having an issue with the following directive code: .directive('myDirective', function () { restrict: 'E', replace: true, transclude: true, scope: { label: '@', ngModel: '=', ...

Guidelines for utilizing React to select parameters in an Axios request

As a newcomer to ReactJs, I am working with a Product table on MySQL. I have successfully developed a dynamic table in the front-end using ReactJS along with MySQL and NodeJs on the backend. The dynamic table consists of four columns: Product, Quantity, Pr ...

Recreating dropdown menus using jQuery Clone

Hey there, I'm facing a situation with a dropdown list. When I choose "cat1" option, it should display sub cat 1 options. However, if I add another category, it should only show cat1 options without the sub cat options. The issue is that both cat 1 a ...