2017-03-16 3 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 인 경우 문제가 발생합니다. 구독하려고하면 정의되지 않은 구독을 할 수 없다고 말합니다. 나는 이것이 중첩 된 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; 
     }); 
    } 
} 
관련 문제