Troubleshooting the issue of the Delete Service not functioning properly within Angular

ListStore.ts file

export class ListstoreComponent implements OnInit {
  rawlist;
  name = '';
  id = '';
  storeid = "";
  store: Store;

  constructor(private api: APIService, private router: Router, private route: ActivatedRoute, private cookieService: CookieService) { }

  ngOnInit() {
    this.fetchStoreList();

  }

  //fetch store list
  async fetchStoreList() {
    this.rawlist = await this.api.ListStores();
    this.storelist = this.rawlist.items;
  }
  //delete store
  async deleteStore(id) {
  if (window.confirm('Are you sure, you want to delete?')){
    await this.api.DeleteStore(id).catch(data => {
      this.refreshStoreList();
    })
  }
} 

HTML File

 <button type="button" class="btn btn-primary btn-gap" (click)="deleteStore(Store.id)">Close Ticket</button>

I'm removing the list and updating database. I am uncertain whether catch() would function as expected. I attempted subscribe but it did not work.

Answer №1

Your event handler needs to handle the asynchronous nature of the call instead of passing it up to the HTML content.

deleteStore(id): void {
  if (!window.confirm('Are you sure, you want to delete?')){
    return;
  }

  this.api.DeleteStore(id).then(message => {
    // handle response
  });
}

Edit:

You could manage it in the HTML, but you'd have to return a promise from the event handler, leading to more code cluttering your HTML structure.

If you prefer using the async await syntax, you can achieve the same functionality with the following approach:

async deleteStore(id): void {
  if (!window.confirm('Are you sure, you want to delete?')){
    return;
  }

  const message = await this.api.DeleteStore(id);
  // handle response
}

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

Navigating in Angular is made easy with the Angular routing feature, which allows you to redirect

I have been working through the Angular Tour of Heroes Guide and encountered the section on the "default route". I decided to experiment by removing the pathMatch attribute from the Route associated with an empty string. Below is the code snippet in quest ...

Experiencing an issue with type error when transitioning syntax from using require to import

After transitioning from require in CommonJS syntax to import in ES Module syntax, I encountered an error. I decided to develop a todo-app using Node.js, TypeScript, and MySQL. To start off, I wrote the initial code snippets below: // db.ts export {}; co ...

Utilize an alias to define the SCSS path in an Angular-CLI library project

I am in the process of developing a library project using angular-cli. I have been following the guidelines outlined in the angular documentation. This has resulted in the creation of two main folders: one is located at ./root/src/app, where I can showcase ...

Learn the process of invoking Firebase Functions through AngularFire

My project involves creating a Firebase Cloud function using functions.https.onRequest to act as an API that returns JSON data from the Firebase Realtime database. After some research, I discovered functions.https.onCall, which provides authentication fun ...

Error: Missing 1 type argument(s) in generic type definition

I've developed an abstract class structure as shown below: export abstract class CsvFileReader<T> { data: T[] = [] constructor(public file: string) {} abstract mapRow(row: string[]): T read() { this.data = this.file .split(& ...

Having trouble resolving npm install -g @angular/cli due to issue with checking the installable status of [email protected] module

When attempting to install @angular/cli using the npm command in the command prompt, I encountered an issue with resolveWithNewModule. It seems to be stuck at checking installable status.[email protected] ...

Having trouble with subscribing to a template in Ionic framework

I'm attempting to showcase an image from Firebase storage using the following code: Inside my component : findImg(img) { this.storage.ref('/img/' + img).getDownloadURL().subscribe( result => { console.log(result); ...

The issue encountered is: "Unable to assign property 'id' to a numeric value of '1' in Angular."

In my Angular 7 project, I am trying to establish a client-side request to the server-side. Below is the structure of the request that needs to be sent. { "title" : "Test Title", "user": { "id" : 7 ...

Is it possible for Angular 2 JWT to have an unauthenticatedRedirector feature?

Having transitioned from Angular 1 where JWT tokens were used for user authentication, I had the following code: .config(function Config($httpProvider, jwtOptionsProvider) { // Interceptor to add token to every $http request jwtOptionsProv ...

Creating an interface in Dart: Step-by-step guide to defining interfaces similar to TypeScript

Coming from a Typescript background, I used to define object interfaces like this: export interface Locale { login: { title: string; actions: { submit: string; forgot: string; } } } However, in Dart, interfaces are implicit an ...

Tips for modifying date format in Angular 8

My datepicker for a date column is displaying the incorrect date format after submission. I am looking to change this format to the correct one. I am working with bsConfig bootstrap in Angular 8, but I am unsure of how to modify the date format. The back ...

What is the best way to prevent updating the state before the selection of the end date in a date range using react-datepicker?

Managing user input values in my application to render a chart has been a bit tricky. Users select a start date, an end date, and another parameter to generate the chart. The issue arises when users need to edit the dates using react-datepicker. When the s ...

Issue with TypeScript version 4.2.1 - The Array.some() method does not support a function that returns a boolean

I encountered a TypeScript error that goes as follows: The complete error message reads: Supplied parameters do not match any signature of call target: parameter type mismatch. > Parameter 'Predicate' should have type assignable to {(value: ...

A TypeScript function that strictly checks for tuples without any union elements

Calling all TypeScript gurus! I am currently developing a versatile TypeScript function that can handle two different types of arguments: Class A and B. A and B are completely independent and not related through inheritance. The challenge I am facing is ...

Exploring the Wonderful World of Styled Components

I have a query regarding styled components and how they interact when one is referenced within another. While I've looked at the official documentation with the Link example, I'm still unclear on the exact behavior when one styled component refe ...

Best practices for organizing API Services using TypeScript and Next.js Server Actions

My product-actions/index file contains various server actions such as createProduct and getProductAssets, each of which verifies the user session before processing the request. I am looking for a way to check the session validity only once and then procee ...

Can NgZone be utilized within a shared service instance?

I am currently working on creating an injectable singleton service for my application that will provide all components with information about the window width and height, as well as notify them when the page is scrolled or resized. Below is the code snipp ...

Angular 6's inability to subscribe to a subject from multiple components simultaneously is causing issues

Having an issue with data subscription between parent and child components and a service. When emitting data using the next method in the subject class, only the child component receives the data. In navbar.component.ts: this.messageService.setClientUser ...

How to reveal hidden Div element at a specific index with Angular Material table

In my mat-table, there are several functionalities available: 1. The ability to add or remove rows 2. Adding data into a row using different controls such as combo-boxes, text boxes, etc. One of the controls is a text box labeled "Additional Information ...

Is Angular Signals on track to potentially supplant NgRx in some aspects?

After diving into Signals that are set to be introduced in Angular 16, I have already started coding with it and find it pretty amazing! I've heard rumors (and believe them) that Signals will likely replace most of the RxJS code, except for asynchron ...