Andurit Andurit - 3 months ago 12
TypeScript Question

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

I try simple app where I delete user after clicking on delete button.

When I try to run server like this I get error on

then
in
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: Property 'then' does not
exist on type 'void'.


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


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

Any hint what I'm doing wrong?

Answer

The deleteUser method in your service doesn't return a Promise, so you can't call .then(res => ...) on it, like the error states.

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