Home > Net >  I'm traying to list from json server to a table
I'm traying to list from json server to a table

Time:12-26

getting the error trying to declare d in subscribe

this is my service

public apiUrl = 'http://localhost:3000/';

constructor(public http: HttpClient) {}

KayitListele() {
 return this.http.get(this.apiUrl   'kayitlar');
}

from ts file of the component, function to list

KayitListele() {
 this.servis.KayitListele().subscribe((d: Kayit[]) => {
  this.kayitlar = d;
 });
}

error message

No overload matches this call.
  Overload 1 of 5, '(observer?: PartialObserver<Object>): Subscription', gave the following error.
    Argument of type '(d: Kayit[]) => void' is not assignable to parameter of type 'PartialObserver<Object>'.
      Property 'complete' is missing in type '(d: Kayit[]) => void' but required in type 'CompletionObserver<Object>'.
  Overload 2 of 5, '(next?: (value: Object) => void, error?: (error: any) => void, complete?: () => void): Subscription', gave the following error.
    Argument of type '(d: Kayit[]) => void' is not assignable to parameter of type '(value: Object) => void'.
      Types of parameters 'd' and 'value' are incompatible.
        The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
          Type 'Object' is missing the following properties from type 'Kayit[]': length, pop, push, concat, and 26 more.ts(2769)
types.d.ts(73, 5): 'complete' is declared here.

i'm going through of a course and after i get this error, i wrote exactly same code, still getting same error.

CodePudding user response:

Modify your service method like below and it should work.

KayitListele(): Observable<Kayit[]> {
    return this.http.get<Kayit[]>(this.apiUrl   'kayitlar');
}

When you are consuming the method you are saying that this is the return type I am expecting so in that case explitly declare the same type in your service method as well.

  • Related