Angular6 Type 'Observable' is not assignable to type 'Observable'

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Angular6 Type 'Observable<Object>' is not assignable to type 'Observable<…>'



Context to my problem: I am trying to make an http request in the typescript part of my Angular app. I am just trying to call the localhost:5000 for data from my Python REST API.



I believe the following two components are all that is needed to solve this problem. I just can't see what can be replace or installed to fix this. Please let me know.



exam.model.ts


// TS class to represent coordinate data

export class Exam
constructor(
public Company: string,
public HireDate: string, // should be changed to date
public Title: string,
public Location: string,
public Degree: string,
public yearEnteredWorkForce: string, // should be changed to date
public OfferCostNumber: string,
public bonus: string,
public latitude: number,
public longitude: number
)



exam-api.service.ts


import Injectable from '@angular/core';
import HttpClient, HttpErrorResponse from '@angular/common/http';
import Observable from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import API_URL from '../env';
import Exam from './exam.model';

@Injectable()
export class ExamsApiService any) 'Error: Unable to complete request.');


// GET list of public, future events
getExams(): Observable<Exam>
return this.http
.get(`$API_URL/data`)
.catch(ExamsApiService._handleError);




Here is the entire error I am getting:


ERROR in src/app/http-example/exam-api.service.ts(20,5): error TS2322: Type 'Observable<Object>' is not assignable to type 'Observable<Exam>'.
Type 'Object' is not assignable to type 'Exam'.
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Property 'includes' is missing in type 'Object'.





By default http.get is an observable of an object. Your method signature says you should get back an observable of an array, which is a different type. You should specify the type of the response object so TS can help you: angular.io/guide/http#type-checking-the-response
– jonrsharpe
Aug 6 at 16:23



http.get




1 Answer
1



Above mentioned 'Exam' Class is not assignable to Observable, as class cannot be used a interface.



Make a interface like this :


export interface Exam
Company: string,
HireDate: string,
Title: string,
Location: string,
Degree: string,
yearEnteredWorkForce: string,
OfferCostNumber: string,
bonus: string,
latitude: number,
longitude: number





Then use it in Observable type.



Try This!!, Hope this should help





Hey Thanks! Didn't work though. I'm getting the same error still.
– Enesxg
Aug 6 at 19:46





Were there any other changes that were supposed to also be made?
– Enesxg
Aug 6 at 19:46





Why do you think they don't already have this type, given that they're using it? And this doesn't tell the OP how to actually use this with the client.
– jonrsharpe
Aug 6 at 20:27





I think you need to replace Observable<Exam> with Observable<HttpResponse<any>>
– Danish Arora
Aug 7 at 6:31





I think you need to replace Observable<Exam> with Observable<HttpResponse<any>>... That will remove the error and at the time of subscription to the observable use above mentioned interface as response type..
– Danish Arora
Aug 7 at 6:33






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS