0

I have an Interface

export interface IEmployee{
  id: string;
  first_name: string;
}

In my employee.service.ts

  getEmployess():Observable<IEmployee>{
    return this._http.get<IEmployee>(this._dataUrl);
  }

In my component

  employees:Array<IEmployee>;   

  constructor(private _employeeService:EmployeeService){
    this._employeeService.getEmployess()
      .subscribe(
        (data) => {
          this.employees = data;
          console.log(data);
        }
      )
  }

I am gettign the erro [ts] Type 'IEmployee' is not assignable to type 'IEmployee[][]'.

I am unable to understand whats wrong. I want that the returned data from service should be stored in employees array.

Please help.

1
  • Your service return type is Observable<IEmployee>. So, each event emitted by this observable is one IEmployee. You're trying to store that IEmployee into a variable of type Array<IEmployee>. That can't work. An egg box is not the same thing as an egg. Commented Mar 1, 2018 at 9:12

2 Answers 2

2

return

 getEmployess():Observable< Array<IEmployee>>{
    return this._http.get<Array<IEmployee>>(this._dataUrl);
  }

or

getEmployess():Observable< IEmployee[]>{
    return this._http.get<IEmployee[]>(this._dataUrl);
  }
Sign up to request clarification or add additional context in comments.

1 Comment

its better to unsubscribe by assigning to one variable
1

Reason of issue:

this.employees = data;  
// this.employees is type of IEmployee[]
// data is type of IEmployee

Solution -> Just change :

getEmployess():Observable<IEmployee>

To

getEmployess():Observable<IEmployee[]>{
// OR
getEmployess():Observable<Array<IEmployee>>{

As you are getting array of IEmployee in return and you have defined employees:Array<IEmployee>;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.