TypeError:无法读取undefined(Observable)的属性'subscribe'

时间:2017-03-16 01:13:38

标签: angular typescript

我有以下(TypeScript / 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承诺。任何想法都非常感激。

1 个答案:

答案 0 :(得分: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);
        });
    });
  }
}

或在所有代码中使用promises:

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;
        });
    }
}