繁体   English   中英

Angular2打字稿错误TS2339:类型“ Y”上不存在属性“ x”

[英]Angular2 typescript error TS2339: Property 'x' does not exist on type 'Y'

我正在尝试将http响应转换为DTO对象,但无法实现。请帮忙。
在这里,我有一些服务,这些服务使用的采用器类将响应转换为对象数组并返回到控制器,同时为该服务分配http服务响应此变量其给定错误。“错误TS2339:类型'属性'collectionArray'不存在采集[]'”

DTO:

export class Collection {

private collectionId: number;
private image: string;
private url:string;
private title:string;
private description:string;

  static getCollection(responseData) {
    var obj = new Collection();
    obj.setCollection_id(responseData.collection_id);
    obj.setImage_url(responseData.image_url);
    obj.setUrl(responseData.url);
    obj.setTitle(responseData.title);
    obj.setDescription(responseData.description);
    return obj;
  };
//getter setter
}


   **Controller:**

        import { Collection } from '../model/collection';

        import {CollectionService} from './collection.service';

         public collections : Collection[] = [];

        //calling service
        this.collectionService.getCollection().subscribe(
              data => {
                 // giving error here
                 this.collections = data.collectionArray
              },
              err => {
                 console.log(err);
              });

        **service :** 

         getCollection() : Observable<Collection[]>{
                 // ...using get request
                 var options = new RequestOptions({
                    headers: new Headers({
                    'Accept': 'application/json',
                     'user-key' : '7d5ef14e15e096400df74871'
                    })
                  });
               return this.http.get(this.url, options)
              //returning arry of colletion object
                 .map((res:Response) =>  new CollectionAdapter(res.json()))
                   .catch((error:any) => Observable.throw(error.json().error || 'Server error'));

                 }

        **Adapter:**

        export class CollectionAdapter{

            public collectionArray : Collection[] = [];

            constructor(responseData){
              this.getCollectionList(responseData.collections);
            };

            getCollectionList(response) {
                for (var collectionObj of response) {
                  let collObj = collectionObj.collection;
                  let obj = Collection.getCollection(collObj);
                  this.collectionArray.push(obj)
                }
                return this.collectionArray;
            };
          }

如果错误在编译

这可能是因为您的服务正在返回:

 getCollection() : Observable<Collection[]>

尝试将其更改为

 getCollection() : Observable<CollectionAdapter>

如果错误发生在运行时

使用console.log(data)查看输出是什么

暂无
暂无

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

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