【问题标题】:switchmap complains not returning but thought i wasswitchmap 抱怨没有返回,但我以为我是
【发布时间】:2022-01-20 12:56:05
【问题描述】:

这是错误:

ERROR TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.

这里是代码

publicresourceuploadtos3(event): Observable<any>{
    console.log('it got inside the public resource upload to s3 ');
    const mediatobeuploaded = event.target.files[0];
    return this.http.get(environment.public_generate_presigned_url_resource).pipe(
      switchMap((req : any)=>{
        const resourceurlprovided = req.uriroot + req.fields.key;
        console.log('it got the public presigned url');

        let fd = new FormData();
        fd.append('acl', req.fields.acl);
        fd.append('key', req.fields.key);
        fd.append('content-type', req.fields['content-type']);
        fd.append('policy', req.fields.policy);
        fd.append('x-amz-algorithm', req.fields['x-amz-algorithm']);
        fd.append('x-amz-credential', req.fields['x-amz-credential']);
        fd.append('x-amz-date', req.fields['x-amz-date']);
        fd.append('x-amz-signature', req.fields['x-amz-signature']);

        fd.append('file', mediatobeuploaded);
        return this.http.post(req.url, fd, {
         reportProgress: true,
         observe: 'events'
       }).pipe(
         // answer two https://stackoverflow.com/questions/51176537/angular-6-and-node-js-aws-s3-file-upload-progress-bar
         switchMap((events: any)=>{
           console.log('it got into the swtich map');
           switch(events.type){
             case HttpEventType.UploadProgress:
               const result = {
                 progressreport: true,
                 progress: Math.round(events.loaded / events.total * 100)
               };
               console.log('it went into upload progress');
               return of(result);
             case HttpEventType.Response:
               const result2 = {
                 progressreport: false,
                 resourceurl: resourceurlprovided,
                 resourcekey: req.fields.key
               };
               console.log('it went into the response');
               return of(result2);
           }
         }));

      }));
  }

注意console.log() 语句。最后一个开火的是console.log('it got into the switch map')

这意味着我的 switch map 调用这个错误是因为它期望返回 observables。

但是看看这段代码。

switch(events.type){
             case HttpEventType.UploadProgress:
               const result = {
                 progressreport: true,
                 progress: Math.round(events.loaded / events.total * 100)
               };
               console.log('it went into upload progress');
               return of(result);
             case HttpEventType.Response:
               const result2 = {
                 progressreport: false,
                 resourceurl: resourceurlprovided,
                 resourcekey: req.fields.key
               };
               console.log('it went into the response');
               return of(result2);
           }
         }));

return of 语句为什么不满足?我究竟做错了什么?我的理解是,返回是一个可观察的。

我的部分思考过程是,如果两个 switch case 语句不起作用,我需要一个默认语句。但我试过了,我的 IDE 一直对我大喊大叫。所以要么我在做一些完全愚蠢的事情,要么我错过了一些重要的事情。

【问题讨论】:

    标签: angular rxjs observable switchmap


    【解决方案1】:
    1. 如果您在switchMap() 中返回of(result),请改用map()。您可以简单地返回结果,而无需将其包装在 of() 中。
    2. 您的 switch 块处理两种情况,但如果两种情况都失败了怎么办?那是您不返回任何东西的时候。如果两种情况都失败,请尝试在最后添加默认情况以返回某些内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-11
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      相关资源
      最近更新 更多