0
votes

I'm trying to learn angular by creating a web application for the parish. in the products-list.component.ts I have the method for deleting which seems to be formally correct, but in the phase of npm start it goes into error with the error code

TS2339: Property 'subscribe' does not exist on type 'Promise '.

I apologize if I do not respect the formal rules for forwarding the problem, but I am new and maybe I am documenting the problem incorrectly. Thanks for your patience. Moreno

goes wrong on the onDelete method

in other classes I use similar code and I haven't had an error of this kind. Are there any imports on list.component-products? Thanks inifinite

import { Component, OnInit, SystemJsNgModuleLoader } from '@angular/core';
import { NgForm } from '@angular/forms';


import { Prodotti } from 'src/app/model/prodotti.model';
import { Router, ActivatedRoute } from '@angular/router';
import { JsonPipe } from '@angular/common';

import { ProdottiListService} from 'src/app/features/prodotti/components/prodotti-list/prodotti-list.service';
import { ToastrService } from 'ngx-toastr';



let Header_Msg = "Gestione Prodotti";


@Component({
  selector: 'app-prodotti-list',
  templateUrl: './prodotti-list.component.html',
  styleUrls: ['./prodotti-list.component.css']
})
export class ProdottiListComponent implements OnInit {

  constructor(private service: ProdottiListService, private toastr: ToastrService) {

   }

  ngOnInit() {
    this.service.refreshList();
  }

  populateForm(emp: Prodotti) {
    this.service.formData = Object.assign({}, emp);
  }

  onDelete(id: number) {

    if (confirm('Confermi la cancellazione del Record ?'))  {
        this.service.deleteProdotti(id).subscribe(res => {
          this.service.refreshList();
          this.toastr.warning('Cancellazione eseguita con successo', Header_Msg);
        })
    }
  }

}

ERROR in src/app/features/prodotti/components/prodotti-list/prodotti-list.component.ts(41,41): error TS2339: Property 'subscribe' does not exist on type 'Promise'.

1
subscribe() is only for Observable<T>. You can use .then() for a promise instead of subscribe(). That being said you should probably share the code for ProdottiListService. - Alexander Staroselsky

1 Answers

0
votes

You need to make sure that the service method returns an Observable not a promise.

In your case ProdottiListService has deleteProdotti which returns a promise so you should be using .then(..) not .subscribe().

Either update your service method to return an observable or do not use .subscribe in component:

this.service.deleteProdotti(id).then(res => {
          this.service.refreshList();
          this.toastr.warning('Cancellazione eseguita con successo', Header_Msg);
        })