2

I'm developing an angled and php application and I'm running it all on localhost. on my front end I have a function that retrieves my information and displays. each line in the list has a button called delete to be able to delete this item, when I click on delete I get the id of the item and send a delete request to my server. I follow this along with the postman and it is possible to visualize that it really deletes all the files, but in my angular the item is still there, even though I remove it from the list when I try to retrieve the list using the method get the deleted item back but in the postman does not return.

Category.service.ts

  get(page, descricao) {
    page = 'all';
    return this.http.get(Auth.url + this.nameClass + `?page=${page}&desc=${descricao}`, { headers: { 'token': Auth.token } });
  }

  delete(id) {
    console.log(Auth.url + this.nameClass + "/" + id);
    this.http
      .delete(Auth.url + this.nameClass + "/" + id, { headers: { 'token': Auth.token } })
      .subscribe(dados => alert('Ok'), (error: any) => alert('erro'));
  }

When I use a function to delete the alert ('ok'), since my server returns true because it deleted perfectly

CategoryList.Component.ts

items: any;
ngOnInit() {
    this.getList();
}

 excluir(desc, id) {

    if (confirm("Do you want to delete the category " + desc + " ?")) {
      let index = this.getIndex(id);
      if (index == -1)
        alert('Error');
      else {
        let result = this.categoriaService.delete(id);
        this.items.splice(index, 1);
      }
    }
  }

  getIndex(id) {
    for (let i = 0; i < this.items.length; i++)
      if (this.items[i].id == id)
        return i;
    return -1;
  }

  getList(page: number = 1) {

     this.categoriaService.get(page, this.pesquisarValue)
.subscribe(dados => this.exibeLista(dados), (error: any) => console.log(error));
      }



  exibeLista(dados) {

    this.items = null;

    if (dados.result.count == 0) // Quantidade de itens retornados da consulta
      this.pages = null;
    else {
      if (dados.result.paginas == 0) // Quantidade de paginas
        this.pages = null;
      else {
        this.totalPages = dados.result.paginas;
        this.loadPages();
      }
      this.items = dados.result.item;
    }

I use the this.items.splice (index, 1) function; and removes the deleted item from my array of items, so far so good. when I leave this page and come back, the item is there again, even though it is not in postman. I am using the extension in the chrome of allow-control-allow-origin will this cause any problems? Is this my list in some cache? how can I delete data that has been deleted

Emiry Mirella
  • 567
  • 1
  • 7
  • 21
  • please don't tag a question with both [tag:angular] and [tag:angularjs] unless you truly are doing a mixed framework project; **These are not the same framework**. – Claies Jul 23 '18 at 13:41
  • Hello, I'm new to angular and I'm going through this same problem. on the server the item is removed, but when the function is called to retrieve it back. however if you try to delete it again it returns as if it had already been deleted – Bruno Jul 23 '18 at 14:12

1 Answers1

0

Your code is looking good except the service .Please change your service code as below

get(page, descricao) {
    page = 'all';
    return this.http.get(Auth.url + this.nameClass + `?page=${page}&desc=${descricao}`, { headers: { 'token': Auth.token } })
.map(response => return response ) // or try  `response.json()` instead of `response` 
.catch(error => {
     return Observable.throw(error);
   });;
  }
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • Hi, I'm trying to use your method and the following error is displayed: "Map property does not exist in type Observable", my imports import { Observable } from '../../../node_modules/rxjs'; import { map } from 'rxjs/operators'; – Emiry Mirella Jul 23 '18 at 13:40
  • @EmiryMirella Just import `import 'rxjs/Rx';` https://stackoverflow.com/questions/37208801/property-map-does-not-exist-on-type-observableresponse – Ramesh Rajendran Jul 23 '18 at 13:44
  • My angle is version 6. I did it, but still the error "ERROR in node_modules / rxjs / Rx.d.ts (1.15): error TS2307: Can not find module 'rxjs-compat' still exists. src / app / model / category.service.ts (35,6): error TS2339: Property 'map' does not exist on type 'Observable '. " – Emiry Mirella Jul 23 '18 at 13:50
  • Then you should fix the error. Because every service call we need to be use the `map()` for observable ., – Ramesh Rajendran Jul 23 '18 at 13:51
  • can I use the map inside the pipe? – Emiry Mirella Jul 23 '18 at 13:52
  • You can return the observer with map and even then the incorrect value continues, there are 9 items I delete 1. in my postman it gets 8 but in my getList it returns me 9 – Emiry Mirella Jul 23 '18 at 14:09
  • Just FYI ... since Angular v4.3 we no longer need to use `map` on the response. – DeborahK Jul 24 '18 at 21:01