data.map不是一个函数 - Angular 2

时间:2018-02-05 09:33:55

标签: angular angular2-services

我正在尝试将json结果分配给组件中的数组对象。我收到以下错误。

data.map is not a function

我已在我的服务中导入import 'rxjs/add/operator/map';,但仍然收到相同的错误。 我使用相同的代码从另一个服务中提取另一组数据,但代码工作正常。

ArticleDetailService.ts

import {Injectable} from '@angular/core';
import {Http, Response, HttpModule} from '@angular/http';
import 'rxjs/add/operator/map';
import {HttpClientModule} from '@angular/common/http';

@Injectable()
export class ArticleDetailService {

    constructor(private http: Http) {
    }

    getArticleDetail() {
        return this.http.get('APIURL')
            .map((res: Response) => res.json());
    }

}

ArticleDetailsComponent.ts

import {Component, OnInit} from '@angular/core';
import {ArticleDetailService} from '../Services/article-detail.service';
import {ArticleDetail} from './articledetails';


@Component({
    selector: 'app-article-detail',
    templateUrl: './article-detail.component.html',
    styleUrls: ['./article-detail.component.css']
})
export class ArticleDetailComponent implements OnInit {

    obj: Array<ArticleDetail> = [];

    constructor(private _sharedService: ArticleDetailService) {
    }

    ngOnInit() {
        this.getArticleDetailfromService();
    }

    getArticleDetailfromService() {
        this._sharedService.getArticleDetail()
            .subscribe(data => {
                    this.obj = data.map(e => {
                        return {Abstract: e.Abstract, ImageLink: e.ImageLink}
                    });
                },
                (err) => console.log(err),
                () => console.log(this.obj)
            );
    }

}

ArticleDetails.ts

export class ArticleDetail{
ArticleId:string;
Abstract:string;
ArticleXML:string;
UpdatedDate:string;
ImageLink:string;
SectionName:string;
}

0 个答案:

没有答案