1

I'm trying to load some data from a service, the service works as i tested it in the console, it displays the wanted array properly, but when calling the same method of the service from my component (where i need to render the data) it just displays [object Object].... in the console and nothing on the component itself.

FYI : the provider ('/api/departement') that am using works properly since the data can be displayed in the console (data from the service itself).

here's my code :

Department.ts :

export class Departement{
public code_dep:String;
public lib_dep_AR:String;
public lib_dep_FR:String;
public countm:number;
}

Departement.service.ts

import { Departement}  from './departement';
import { Http,Headers } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import { Injectable } from '@angular/core';
@Injectable()
export class DepartementService{

constructor(private http:Http){}
private headers = new Headers({'Content-type':'application/json'});
private depUrl = '/api/departement';

getAllDeps():Promise<Departement[]>{

    return this.http.get(this.depUrl + "/allDeparts").toPromise().
    then(response=>response.json() as Departement[]);  

  }
// etc
}

displaying.component.ts

  export class Displaying implements OnInit {
  departements:Departement[];
  departement:Departement;
  constructor(public depserv:DepartementService,public router:Router) {

   }

  ngOnInit() {
    this.depserv.getAllDeps().then(dep =>    
      {    this.departements = dep;
       });

    //etc  
  }

where i want to render the result

<select name="budget" >     
 <option>my results down here</option>            
 <option *ngFor="let x of departements">{{x.lib_dep_FR}}</option>
</select>

fixed by moving on to the Observable interface

Department.service.ts

private extractData(res:Response) {
    let body = res.json();
    return body || [];
}
getData():Observable<Departement[]>{
    return this.http.get(this.depUrl+"/allDeparts").map(this.extractData).catch(this.handleError);
}

catching up the result in the displaying.component.ts

ngOnInit(){
    this.depserv.getData().subscribe(
   dept => this.departements = dept);
}
Poul Kruijt
  • 69,713
  • 12
  • 145
  • 149

1 Answers1

0

In displaying.component.ts file instead of this this.departements = dep; change to this this.departements = dep.json(); and just check what you are getting in console.log(this.departements) If you are getting array of objects or array of values then data will come for x.lib_dep_FR

Rakesh A R
  • 186
  • 1
  • 2
  • 17
  • the .getAllDeps() method is already returning an array "Promise : Department[]" , so i can't use dep.json() while fetching in the component – Yassine Ben Hamida Feb 21 '18 at 13:04
  • In displaying.component.ts file are you getting data in dep ?? – Rakesh A R Feb 21 '18 at 13:06
  • yes am getting : data : [object Object],[object Object],[object Object],[object Object],[object Object],[object Object] – Yassine Ben Hamida Feb 21 '18 at 13:11
  • @YassineBenHamida above you told you are getting undefined – Sajeetharan Feb 21 '18 at 13:26
  • Because you don't have JSON. You have an array: []. JSON is a string representation of a javascript object. You could use the JSON.stringify method to generate a JSON string from an existing object: . change to this.departements =JSON.stringify(dep); and now check what you are getting in console.log( this.departements) – Rakesh A R Feb 21 '18 at 13:37