MMR
MMR

Reputation: 3009

How to get the data from db

I have an API by which i want to display data by certain conditions,here i am getting the details as,

My service Component,

    @Injectable()
    export class GetAllList {
    id = localStorage.getItem('social_id')
    private _productUrl =    'http://localhost/a2server/index.php/profile/getProfile/'+this.id;
   constructor(private _http: Http) { }
    getList(): Observable<IDetails[]> {
    return this._http.get(this._productUrl)
    .map((response: Response) => { 
    return <IDetails[]> response.json(); 
    });
      }
         }

My Observable,

   export interface IDetails{
    profile_id:number;
    firstname: string;
    lastname: string;
    profilename: string;
    phone:string;
    email:string;
    address:string; 
    }

I am using these service in my main component as

 ngOnInit(){
  this._service.getList()
 .subscribe(details => this.details = details); 
  }

This works well,if i want to check firstname in console,how would i do that?Is it like these.....

ngOnInit(){
  this._service.getList()
 .subscribe(details => this.details = details); 
  console.log(this.details[0].firstname);
  }

Can any one suggest help please......

Upvotes: 0

Views: 82

Answers (3)

micronyks
micronyks

Reputation: 55443

Use it as suggested below.

ngOnInit(){

  this._service.getList().subscribe((data) =>{    
    this.details = data; 

    this.details.forEach((detail)=>{
          console.log(detail.firstname);  
    })

  });

}

Upvotes: 0

Chao
Chao

Reputation: 905

ngOnInit(){
  this._service.getList().subscribe(details => { 
    this.details = details; 
    for(let detail of this.details) {
       console.log(detail.firstname);
    }
  });
}

Upvotes: 1

yusijs
yusijs

Reputation: 867

You need to wrap your console.log in curly braces, like this:

    ngOnInit(){
      this._service.getList()
     .subscribe(details => { this.details = details; 
console.log(this.details[0].firstname);
}); }

This makes sure that you don't log it to the console unless you have fetched the data.

Upvotes: 0

Related Questions