2017-03-16 138 views
0

'訂閱' 我有以下的(一個打字稿的一部分/ Angular2/SPFX項目):類型錯誤:無法讀取屬性未定義(可觀察)

// 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。當我嘗試訂閱時,它說不能訂閱undefined。我懷疑這是因爲嵌套的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; 
     }); 
    } 
} 
相關問題