简体   繁体   English

订阅可观察角度未定义

[英]Subscribing on angular observable is undefined

I'm pretty new to angular and now trying to upload a file and when the file is uploaded successfully it shall create an entry in database with the data that is returned by the api. 我对angular非常陌生,现在尝试上传文件,并且文件成功上传后,它将使用api返回的数据在数据库中创建一个条目。

for the upload i'm using 我正在使用的上传

import { FileUploader } from 'ng2-file-upload';

This is my function for uploading: 这是我上传的功能:

uploadSingleFile() {
  if (this.uploader.queue.length <= 0)
    return;

  const file = this.uploader.queue[0];
  const fileItem = file._file;
  const data = new FormData();
  data.append('file', fileItem);

  this.uploadFile(data, () => file.remove()).subscribe((image: any) => {
    this.uploadedFile = image;
  });
}

uploadFile(data: FormData, onSuccess: any): Observable<any> {
  return this.fileService.upload(data, onSuccess, r => { console.error(r); });
}

the fileService looks like: fileService看起来像:

upload(model: any, onSuccess: any = undefined, onError: any = undefined) {
    return this.httpClient
      .post(`${this.baseUrl}upload`, model, { observe: 'response' })
      .pipe(
        map((response: any) => {

          console.log(response.status); 
          if (response.status === 201) { // this works, I'm getting Status 201
            if (onSuccess !== undefined)
              onSuccess(response);
          }
          else if (onError !== undefined)
            onError(response);
        })
      );
  }

the api function that is called: api函数,称为:

[HttpPost, Route("upload")]
public async Task<ActionResult> Upload()
{
    // ...

    FileForUploadResponseDto fileForUploadDto = new FileForUploadResponseDto
    {
        FilePath = fileName,
        CreationDate = DateTime.Now,
        Title = file.FileName,
        Size = fileLength
    };


    return StatusCode(201, fileForUploadDto);
}

It works until this line in uploadSingleFile() 直到uploadSingleFile()这一行都uploadSingleFile()

this.uploadFile(data, () => file.remove()).subscribe((image: any) => {
  this.uploadedFile = image;
});

the variable image is undefined. 变量image未定义。 any idea? 任何想法? I'd like to have here the data sent in my response body. 我想在这里将数据发送到我的响应正文中。

Map operator always work before subscribe. 地图操作员始终在订阅之前工作。 It provide rewrite http response as you want. 它提供所需的重写HTTP响应。 You used "map" operator but did not return anything, so subscribe data is going to be empty. 您使用了“地图”运算符,但未返回任何内容,因此订阅数据将为空。

upload(model: any, onSuccess: any = undefined, onError: any = undefined) {
    return this.httpClient
      .post(`${this.baseUrl}upload`, model, { observe: 'response' })
      .pipe(
        map((response: any) => {

          console.log(response.status); 
          if (response.status === 201) { // this works, I'm getting Status 201
            if (onSuccess !== undefined)
              onSuccess(response);
          }
          else if (onError !== undefined)
            onError(response);
            return response.body; --> you should add this line.
        })
      );
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM