Angular 角度Rxjs forkJoin错误:无法读取属性';订阅';未定义的

Angular 角度Rxjs forkJoin错误:无法读取属性';订阅';未定义的,angular,rxjs,observable,fork-join,forkjoinpool,Angular,Rxjs,Observable,Fork Join,Forkjoinpool,我正在尝试向SharePoint列表项添加多个附件。服务方法接受附件列表,只有在上载了所有文件后,才应返回响应(Sharepoint API一次只接受一个文件)。因此,我使用rxjs forkJoin来实现这个目的 下面是我写的服务类 import { Injectable } from '@angular/core'; import { Observable, forkJoin } from 'rxjs'; import { HttpClient, HttpHeaders } from '@a

我正在尝试向SharePoint列表项添加多个附件。服务方法接受附件列表,只有在上载了所有文件后,才应返回响应(Sharepoint API一次只接受一个文件)。因此,我使用rxjs forkJoin来实现这个目的

下面是我写的服务类

import { Injectable } from '@angular/core';
import { Observable, forkJoin } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http'
import { ContextService } from './context.service'
import { mergeMap } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class AttachmentService {

  constructor(private httpClient: HttpClient) { }

  addAttachmentsToTheList(listName: string, listItemId: number, attachmentList: any[], diagest: any) {
    //get the file upload observable list
    let fileUploadOnservables: any[] = [];
    attachmentList.forEach(file => {
      fileUploadOnservables.push(this.addAttachment(listName, listItemId, file, diagest))
    });

    // use forkJoin to combine the operations and subscribe to all results
      // use forkJoin to combine the operations and subscribe to all results
      forkJoin(fileUploadOnservables)
      .subscribe(
        (response) => {
        //after all the successful requests do something
      }
      );
  }
}

在组件中:

someMethod(){
 this.attachmentService.addAttachmentsToTheList('SomeList', 1, this.attachmentList,diagest)
   .subscribe(
   (result: any) => {
       //do something
    },
    (error: any) => {
       //handle error
    });

}
这段代码抛出以下错误,但它也上传了文件。我做错了什么,如何修复这个错误

Cannot read property 'subscribe' of undefined
感谢您的帮助。

原始答案 错误似乎是由您从组件订阅服务中方法的结果引起的,但是服务中的方法实际上并没有向组件返回任何内容。您应该能够通过从服务中的方法返回
forkJoin
的结果并将
subscribe()
从服务中的方法移动到组件来解决此错误。我建议将返回类型添加到方法中,因为这将有助于将来更好地捕获类似于此的错误

addAttachmentsToTheList(listName:string,listItemId:number,attachmentList:any[],diagest:any):可观察{
//获取文件上载可观察列表
让fileUploadOnservables:any[]=[];
attachmentList.forEach(文件=>{
fileUploadOnservables.push(此.addAttachment(listName、listItemId、file、diagest))
});
//使用forkJoin组合操作并订阅所有结果
返回forkJoin(fileUploadOnservables);
}
如果在fork加入
文件上载服务后需要在服务中执行进一步的操作,则可以使用以下功能:

returnforkjoin(fileUploadOnservables.pipe(…);
最新答案 您可以尝试使用运算符将值列表拆分为单个事件,然后使用运算符组合每个事件

addAttachmentsToTheList(listName:string,listItemId:number,attachmentList:any[],diagest:any):可观察{
从(attachmentList).pipe(mergeMap(value=>this.addAttachment(listName,listItemId,value,diagest))返回,toArray();
}

出于兴趣,堆栈跟踪是否表明错误正在
addAttachmentsToTheList
方法中抛出,或者在调用服务的组件中抛出?我猜attachmentList是空的或未定义的。@AakashGarg附件列表不是空的,代码实际上检查是否为空并在方法顶部返回,即使我没有把它放在这里。你能在循环后给出错误stacktrace和print fileUploadOnservables吗。@tnc1997,堆栈跟踪指示调用服务的组件的错误。但是我所做的基本上是订阅了addAttachmentsToTheList。更新代码以显示forkSubscribe does的实际内容我像这样更新了代码,仍然抛出相同的错误。组件方法的订户从不点击。你能分享服务的更新代码和组件的代码吗?我发布了更新的代码我刚刚发布了我答案的更新,你能试试让我知道吗?
Cannot read property 'subscribe' of undefined