最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Property 'includes' is missing in type 'Subscription' -angular2 - Stack Overflow

programmeradmin1浏览0评论

I have been trying to get an array of objects from node to a service onInit and assign it to ponent. I am able to view data in service but when i try to assign to a variable in ponent, I get below error. I have included my code as well. Please favour on this.All i need is to just take that array from service and assign it to finallist in ponent.

ERROR in 

    C:/Users/girija/Documents/animapp/src/app/ponents/listponent.ts (28,5): Type 'Subscription' is not assignable to type 'any[]'.
      Property 'includes' is missing in type 'Subscription'.

My code is below: app.service.ts:

   public finallist=[]
     getList(){
          return this.http.get('/api/list').subscribe(data=>{
             this.finallist=JSON.parse(data['_body']);
             return this.finallist;
        })
        }

appponent.ts:

  totallist=[];
     ngOnInit() {
        this.totallist=this.someService.getList();

      }

I have been trying to get an array of objects from node to a service onInit and assign it to ponent. I am able to view data in service but when i try to assign to a variable in ponent, I get below error. I have included my code as well. Please favour on this.All i need is to just take that array from service and assign it to finallist in ponent.

ERROR in 

    C:/Users/girija/Documents/animapp/src/app/ponents/list.ponent.ts (28,5): Type 'Subscription' is not assignable to type 'any[]'.
      Property 'includes' is missing in type 'Subscription'.

My code is below: app.service.ts:

   public finallist=[]
     getList(){
          return this.http.get('/api/list').subscribe(data=>{
             this.finallist=JSON.parse(data['_body']);
             return this.finallist;
        })
        }

app.ponent.ts:

  totallist=[];
     ngOnInit() {
        this.totallist=this.someService.getList();

      }
Share Improve this question asked Aug 20, 2017 at 13:37 GayathriGayathri 1,9066 gold badges28 silver badges57 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

subscribe returns Subscription object, this is not what you have looking for.

Try something like this:

app.service.ts

getList(): Observable<any> {
  return this.http.get('/api/list').map(data => data['_body']);
}

app.ponent.ts

totallist=[];

ngOnInit() {
  this.someService.getList().subscribe(data => this.totallist = data);
}

You should be using a map operator to convert the response into json type

getList(){
          return this.http.get('/api/list')
             .map(response=> <Type>response.json()) /////////
             .subscribe(data=>{
             this.finallist=JSON.parse(data['_body']);
             return this.finallist;
        })
        }

Note : <Type> is used for strict typing

See my answer below. It explains to you how to get your data from the server, to service, and finally to ponent. Receiving Jason instead of html. I am using mongodB but your backend may be different. Hope this helps.

发布评论

评论列表(0)

  1. 暂无评论