Search code examples
angulartypescriptangular-httpclient

Type 'HttpEvent<name[]> is not assignable to type 'name[]'


this is my code:

export interface act {
  id: number;
  name: string;
}
public list!: act[];
getAll(token:any): void{
  this.http.get<act[]>('http://localhost:4200/test', token)
  .subscribe(
    (val) =>
      this.list = val
  );
}

And I'm getting the following error on this.list = val:

Type 'HttpEvent < act[] >' is not assignable to type 'act[]'. Type 'HttpSentEvent' is missing the following properties from type 'act[]': length, pop, push, concat, and 16 more.

I tried searching on google, reading on stack but had no luck. I found similar questions but some are not solved and others are different.

Hope you can help me, thanks in advance.


Solution

  • You're trying to send a request body, but GET requests don't have a body. The second argument of HttpClient.get() is not the body, it's a set of options affecting, among other things, what the method returns.

    So you should use POST or PUT instead of GET.

    And you should also avoid using the any type, to make everything more understandable for you and for the compiler (which would have provided a more focused error message if you had used a properly defined interface instead of any).