2017-03-16 11 views
0

私は(活字体/ Angular2/SPFXプロジェクトの一部)は、次のいる:TypeError例外:プロパティを読み取ることができません(観測可能な)未定義の '購読'

// Populate the regulatory documents dropdown 
this.regulatoryDocumentsService.fetchRegulatoryDocumentsData().subscribe(
    data => { this.regulatoryDocumentsData = data }, 
    err => { window.console && console.log(err) } 
); 

:しかし

public fetchRegulatoryDocumentsData(): Observable<RegulatoryDocument[]> { 
    var choicesArray: Array<RegulatoryDocument> = new Array<RegulatoryDocument>(); 

    // Local environment 
    if (Environment.type === EnvironmentType.Local) 
    { 
     // Send dummy data to the form - this works 
     window.console && console.log("fetchRegulatoryDocumentsData(): Local workbench in use"); 
     choicesArray.push(new RegulatoryDocument(1,"Document 1")); 
     choicesArray.push(new RegulatoryDocument(2, "Document 2")); 
     choicesArray.push(new RegulatoryDocument(3, "Document 3")); 
     return Observable.of<RegulatoryDocument[]>(choicesArray); 
    } 
    else if (Environment.type == EnvironmentType.SharePoint || 
      Environment.type == EnvironmentType.ClassicSharePoint) 
    { 
     // fetchRegulatoryDoocumentsData() is undefined. when I try to subscribe to it. 
     pnp.sp.web.lists.getByTitle('Regulatory Documents').items.get().then((choices: any[]) => { 
      window.console && console.log("choices ..."); 
      window.console && console.log(choices); 

      if (choices) { 
       //choices.forEach((choice) => { 
       // choices.push(new Rating(choice)); 
       //}); 
       //Array.from(choices).forEach(function (child) { 
       // window.console && console.log(child) 
       //}); 

       [].slice.call(choices).forEach(choice => { 
        choicesArray.push(new RegulatoryDocument(choice.ID, choice.Title)); 
       }); 
      } 
      return Observable.of<RegulatoryDocument[]>(choicesArray); 
     }); 
    } 
} 

Environment.type != EnvironmentType.Localの場合に問題が発生します。私が購読しようとすると、それは未定義に加入できないと言います。私はこれがネストされたPNPの約束のためだと思う。任意のアイデアを大いに感謝します

答えて

0

lists.getByTitle('Regulatory Documents').items.get().then(...);あなたのコードでは、観測可能ではなく観測可能なものを保持する約束が返されます。

else if (Environment.type == EnvironmentType.SharePoint || 
      Environment.type == EnvironmentType.ClassicSharePoint) 
    { 
     // fetchRegulatoryDoocumentsData() is undefined. when I try to subscribe to it. 
    return Observable.create((observer: any) => { 
     pnp.sp.web.lists.getByTitle('Regulatory Documents').items.get().then((choices: any[]) => { 



      window.console && console.log("choices ..."); 
      window.console && console.log(choices); 

      if (choices) { 
       //choices.forEach((choice) => { 
       // choices.push(new Rating(choice)); 
       //}); 
       //Array.from(choices).forEach(function (child) { 
       // window.console && console.log(child) 
       //}); 

       [].slice.call(choices).forEach(choice => { 
        choicesArray.push(new RegulatoryDocument(choice.ID, choice.Title)); 
       }); 
      } 
      observer.next(choicesArray); 
     }); 
    }); 
    } 
} 

またはすべてのコード内での約束を使用します。

this.regulatoryDocumentsService.fetchRegulatoryDocumentsData().then(
    data => { this.regulatoryDocumentsData = data }, 
    err => { window.console && console.log(err) } 
); 

サービス

public fetchRegulatoryDocumentsData(): Observable<RegulatoryDocument[]> { 
    var choicesArray: Array<RegulatoryDocument> = new Array<RegulatoryDocument>(); 

    // Local environment 
    if (Environment.type === EnvironmentType.Local) 
    { 
     // Send dummy data to the form - this works 
     window.console && console.log("fetchRegulatoryDocumentsData(): Local workbench in use"); 
     choicesArray.push(new RegulatoryDocument(1,"Document 1")); 
     choicesArray.push(new RegulatoryDocument(2, "Document 2")); 
     choicesArray.push(new RegulatoryDocument(3, "Document 3")); 
     return Promise<RegulatoryDocument[]>.resolve(choicesArray); 
    } 
    else if (Environment.type == EnvironmentType.SharePoint || 
      Environment.type == EnvironmentType.ClassicSharePoint) 
    { 
     // fetchRegulatoryDoocumentsData() is undefined. when I try to subscribe to it. 
     pnp.sp.web.lists.getByTitle('Regulatory Documents').items.get().then((choices: any[]) => { 
      window.console && console.log("choices ..."); 
      window.console && console.log(choices); 

      if (choices) { 
       //choices.forEach((choice) => { 
       // choices.push(new Rating(choice)); 
       //}); 
       //Array.from(choices).forEach(function (child) { 
       // window.console && console.log(child) 
       //}); 

       [].slice.call(choices).forEach(choice => { 
        choicesArray.push(new RegulatoryDocument(choice.ID, choice.Title)); 
       }); 
      } 
      return choicesArray; 
     }); 
    } 
} 
関連する問題