Невозможно получить HttpEventType.Response после HttpEventType.UploadProgress - PullRequest
1 голос
/ 17 июня 2020

Я могу загрузить видео на сервер, после этого я хочу получить ответ от api и pu sh в массив.

Но после загрузки видео не удалось получить ответ

Component.ts


    const fd = new FormData();
    fd.append('file', this.selectedFile);
    this.request = this.createTraining.uploadVideo(fd,{reportProgress: true,
      observe:'events'}).subscribe((event :HttpEvent<any>) =>{

        if(event.type == HttpEventType.UploadProgress){
          this.loaderService.hide();
          this.state = 'in';
          this.name = this.selectedFile.name;
          this.progress= Math.round (event.loaded / event.total*100) ;
          if(this.progress == 100){
            this.show = false;
            this.progress = 0;
            this.state = 'out';
            this.trainingItemList.push({name:this.name,order:null, type:this.selectedFile.type,path:null,question:null});
          }
        }
         **if(event.type == HttpEventType.Response) {
          console.log(event.body.message);**
        }
      },
      error=>{
        console.log(error);
      });
  }

Service

uploadVideo(video,progress):Observable<any>{
    return this.http.post<any>('http://localhost:8080/employee/upload',video,progress);
  }

Я пытаюсь получить ответ после загрузки видео, но не могу войти в

if(event.type == HttpEventType.Response) {
          console.log(event.body.message);

Ответ API после загрузки, который я хочу получить

1 Ответ

0 голосов
/ 17 июня 2020

Свойство message является частью класса HttpErrorResponse. Чтобы получить доступ к телу ответа типа HttpResponse, попробуйте получить доступ только к свойству body.

Заменить

console.log(event.body.message);

с

console.log(event.body);
...