簡體   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