switchmap 抱怨没有返回但以为我是

switchmap complains not returning but thought i was

这里是错误:

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 
         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')

这意味着我的切换映射正在调用此错误,因为它期望 return 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 中的哪些陈述为什么不令人满意?我究竟做错了什么?我的理解是 return 是可观察的。

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

  1. 如果您在 switchMap() 中 returning of(result),请改用 map()。您可以简单地 return 结果而不用 of().
  2. 包装它
  3. 您的开关块处理两种情况,但如果两种情况都失败怎么办?那就是当你没有 returning 任何东西的时候。如果两种情况都失败,请尝试在 return 末尾添加默认情况。