【问题标题】:TypeError: Cannot read property 'subscribe' of undefined (Observable)TypeError:无法读取未定义的属性“订阅”(可观察)
【发布时间】:2017-03-16 01:13:38
【问题描述】:

我有以下(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 出现问题。当我尝试订阅时,它说不能订阅未定义。我怀疑这是因为嵌套的 PNP 承诺。任何想法都非常感谢。

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    您的代码中的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);
            });
        });
      }
    }
    

    或在所有代码中使用 Promise:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-23
      • 2018-02-13
      • 2017-12-25
      • 1970-01-01
      • 2020-06-17
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      相关资源
      最近更新 更多