Angular2 - 'then' does not exist in type 'void'

I am trying a simple application where I delete a user after clicking the delete button.

When I try to start such a server, I get an error message thenin the deleteUser() component:

 deleteUser(user: User, event: any) {
    event.stopPropagation();
    this.userService
      .deleteUser(user)
      .then(res => {
        this.httpUsers = this.httpUsers.filter(h => h !== user);
        if (this.selectedUser === user) { this.selectedUser = null; }
      })
      .catch(error => this.error = error);
  }

service:

  deleteUser(user: User) {
    console.log('Deleting user');
  }

Error message:

app / users.component.ts (46,8): error TS2339: the 'then' property does not exist in the 'void' type.

Line 46 from error one above with .then(res => {

While googling, I found this question , so I removed void from the deleteUser function, however, nothing has changed.

Any hint on what I'm doing wrong?

+4
source share
3 answers

, , .then(,

deleteUser(user: User) {
   return this.http.delete(url+id,...).toPromise();
}
+4

, . , REST API,

  getAudits() {
    // console.log('audits' + amplify.store('audits'));
    let audits: Audit[] = amplify.store('audits'));    
    return Promise.resolve(audits);
  }

this.auditService.getAudits().then(audits => {
  this.audits = audits;
  this.updateChart(true);
});

  getAudits() {
    return this.http.get('/rest/batch').map(res => res.json());
  }

this.auditService.getAudits().subscribe(audits => {
  this.audits = audits;
  this.updateChart(true);
});
+4

deleteUser Promise, .then(res => ...) , , .

deleteUser(user: User) {
  console.log('Deleting user'); // <-- no return call - defaults to void
}
+2

Source: https://habr.com/ru/post/1652349/


All Articles