Search code examples
angulartypescriptrxjsrxjs-observablesswitchmap

using a function that calls observable with 3 different observables tied together with switch map. results in error


I have a function that runs when a file is uploaded to a html form.

uploadidnexttofacepicture(event){
    let subscription = this.s3service.publicresourceuploadtos3(event)
      .subscribe((req: any)=>{
        console.log(req);
      });
    
  }

this function calls a function publicresourceuploadtos3 from a injected service. this function is displayed below:

publicresourceuploadtos3(event): Observable<any>{
    console.log('the service at least ran');

    const mediatobeuploaded = event.target.files[0];
    this.http.get(environment.public_generate_presigned_url_resource).pipe(
      switchMap((req : any)=>{
        console.log('did the call to the server');

        const resourceurl = req.uriroot + req.fields.key;

        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);
        this.http.post(req.url, fd).pipe(
          switchMap((req2: any)=>{
            const result = {
              resourceurl : resourceurl,
              resourcekey: req.fields.key
            };
            return of(result);


        }));


      }));


  }

the console log console.log('the service at least ran'); does fire in the console but the server call one: console.log('did the call to the server'); did not.

but this results in an error:

ERROR TypeError: Cannot read property 'subscribe' of undefined
    at ContentcreatorverificationComponent.uploadidnexttofacepicture

Im worried this is a syntax issue, would be helpful if any of you know what happening.


Solution

  • try to add return before this.http.get(environment.public_generate_presigned_url_resource)

    it should be like this

     publicresourceuploadtos3(event): Observable<any>{
        console.log('the service at least ran');
    
        const mediatobeuploaded = event.target.files[0];
       return this.http.get(environment.public_generate_presigned_url_resource).pipe(
          switchMap((req : any)=>{
            console.log('did the call to the server');
    
            const resourceurl = req.uriroot + req.fields.key;
    
            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);
            this.http.post(req.url, fd).pipe(
              switchMap((req2: any)=>{
                const result = {
                  resourceurl : resourceurl,
                  resourcekey: req.fields.key
                };
                return of(result);
    
    
            }));
    
    
          }));
    
    
      }