简体   繁体   English

如何从数据库获取数据

[英]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, 我有一个API,我想通过它在某些条件下显示数据,在这里我得到的详细信息如下:

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...... 任何人都可以建议帮助吗……

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

You need to wrap your console.log in curly braces, like this: 您需要使用大括号将console.log包裹起来,如下所示:

    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. 这可以确保除非已获取数据,否则不要将其记录到控制台。

Use it as suggested below. 按照以下建议使用它。

ngOnInit(){

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

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

  });

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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