简体   繁体   中英

Type Observable<Object> is not assignable to type Observable

GetUserDetailsById(Id): Observable<userdetails> {

    return this.httpclient.get(this.BaseURI + '/user/UserDetailsById/' + Id).map((response: Response) => <userdetails>response.json())
  }

1. constructor(private httpclient: HttpClient)

2. export class userdetails
{
  idUser: number;
  firstName: string;
  lastName: string;
  email: string;
  permissions: string;
  isActive: boolean;
  displayRole: number;
}

Getting error while removing.map((response: Response) => response.json())

Error: Type Observable<Object> is not assignable to type Observable I dont want to use.map((response: Response) => response.json()) Any suggestions? This above code is working fine when using with this.http example:

return this.http.get(this.BaseURI + '/user/UserDetailsById/' + Id).map((response: Response) => response.json())

  • constructor(private http: Http)

This is the correct syntax:

getFromHttp(): Observable<ResponseAfterMap> {
return this.http.get<ResponseFromHttp>(this.BaseURI + '/user/UserDetailsById/' + Id).map((response: ResponseFromHttp) => response.json())
}

You need to define the generic type of the Observable.

In your case, simply add the Object type:

GetUserDetailsById(Id): Observable<Object> {
    /* ... */
}

But, if you know how the object is going to be, you might want to return some other type, like an interface for example.

interface myInterface {
    id: number;
    name: string
}

GetUserDetailsById(Id): Observable<myInterface> {
    /* ... */
}

If you are unsure of what the type is going to be, you can return any ( but this is not recommended ).

GetUserDetailsById(Id): Observable<any> {
    /* ... */
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM