2016-09-25 24 views
1

jsonオブジェクトをREST APIに送信するために使用できるクラスを作成しようとしています。これは私が送る必要のあるjsonオブジェクトです。JSONオブジェクトへの角度2 /タイプスクリプトクラス

{ 
    "libraryName": "temp", 
    "triggerName": "trigger", 
    "currentVersion": "1.3", 
    "createdUser": "xyz", 
    "visibilityType": "private", 
    "WFAllowdTeam": { 
    "allowedTeam": "team1" 
    }, 
    "WFLibraryHistory": { 
    "createdDate": "2016-7-7T05:10:04.106Z", 
    "modifiedDate": "2016-7-9T05:10:04.106Z" 
    } 
} 

私はこのようなクラスを作成しようとしたオブジェクトthis.library.WFAllowdTeam.WFAllowdTeam = 'team';を作成することで、データを設定しようとした、エラーは、

platform-browser.umd.js:937 TypeError: Cannot set property 'WFAllowdTeam' of undefined 
    at WFLibraryComponentAddNewWorkflow.createWorkflow (wf-library.component.new.workflow.ts:47) 
    at DebugAppView._View_WFLibraryComponentAddNewWorkflow0._handle_click_61_0 (WFLibraryComponentAddNewWorkflow.ngfactory.js:488) 
    at eval (core.umd.js:12718) 
    at SafeSubscriber.schedulerFn [as _next] (core.umd.js:9181) 
    at SafeSubscriber.__tryOrUnsub (Subscriber.ts:240) 
    at SafeSubscriber.next (Subscriber.ts:192) 
    at Subscriber._next (Subscriber.ts:133) 
    at Subscriber.next (Subscriber.ts:93) 
    at EventEmitter.Subject._finalNext (Subject.ts:154) 
    at EventEmitter.Subject._next (Subject.ts:144) 
ある

class WFLibraryHistory { 
     public createdDate: any; 
     public modifiedDate: any; 
    } 

    class WFAllowdTeam { 
     public WFAllowdTeam: string; 
    } 

    export class Library { 
     public libraryName: string; 
     public triggerName: string; 
     public currentVersion: string; 
     public createdUser: string; 
     public visibilityType: string; 
     public libraryID: string; 
     WFLibraryHistory: WFLibraryHistory; 
     WFAllowdTeam: WFAllowdTeam; 
    } 

、私はここで作成したクラスを見つけてください。

この問題を解決するための助力は本当に感謝しています。

答えて

4

これらの(クラス)メンバーを最初にインスタンス化する必要があります。

export class Library { 
    public libraryName: string; 
    public triggerName: string; 
    public currentVersion: string; 
    public createdUser: string; 
    public visibilityType: string; 
    public libraryID: string; 
    WFLibraryHistory: WFLibraryHistory; 
    WFAllowdTeam: WFAllowdTeam; 

    constructor() { 
     this.WFLibraryHistory = new WFLibraryHistory(); 
     this.WFAllowdTeam = new WFAllowdTeam(); 
    } 
} 
+0

ありがとう、これは動作します。 :) – d7k

1

WFAllowdTeamのインスタンスを作成してからプロパティを変更する必要があります。

this.library.WFAllowdTeam = new WFAllowdTeam(); 
this.library.WFAllowdTeam.WFAllowdTeam = 'team'; 
+0

回答ありがとうございます@ブラッド:) – d7k

関連する問題