1

I'm trying to compress an image before making an http post call

 this.activityService.addCourse(
      course,
      fileToUpload
    ).subscribe(
      (result) => {
          console.log(result);
          this.handleSuccess('course_added_successfully');
      },
      error => {
        this.handleError("an_error_occured");
      }
    );

And in the activityService.addCourse :

return this.imageService.compressImage(fileToUpload).map(result => {
      if (fileToUpload)    {
        fileToUpload = result;
        input.append("file", fileToUpload);
        input.append("filetype_id", String(0));
      }
      if (typeof result.name !== 'undefined' && typeof result.size !== 'undefined' && typeof result.type !== 'undefined') {
        this.http.post(Constants.URL_ADD_COURSE, input)
          .map(FunctionsService.extractData).catch(FunctionsService.handleError);
      }
      else {
        Observable.throw('Error compressing image');
      }
});

When debugging i can see that the call arrives to this.http.post(Constants.URL_ADD_COURSE, input) and the returned value is successful, but the call is simply not being made (In Inspect element > Network i can see that nothing happened)

1 Answer 1

1

I notice you never subscribe to the this.http.post observable. That is a cold observable meaning that it won't actually do anything until someone subscribes to it. If you really don't care about the the result of the call itself you can also call .publish() which turns it into a hot observable instead.

EDIT: One option is to call switchMap instead of map. That way you can be sure that the http request was completed before continuing with the rest of the processing.

return this.imageService.compressImage(fileToUpload).switchMap(result => {
  if (fileToUpload)    {
    fileToUpload = result;
    input.append("file", fileToUpload);
    input.append("filetype_id", String(0));
  }
  if (typeof result.name !== 'undefined' && typeof result.size !== 'undefined' && typeof result.type !== 'undefined') {
    return this.http.post(Constants.URL_ADD_COURSE, input)
      .map(FunctionsService.extractData).catch(FunctionsService.handleError);
  }
  else {
    return Observable.throw('Error compressing image');
  }
});

If you really want to treat is an unimportant 'side call' and ignore the results you can just use

this.http.post(Constants.URL_ADD_COURSE, input)
 .map(FunctionsService.extractData)
 .catch(FunctionsService.handleError)
 .publish();
Sign up to request clarification or add additional context in comments.

2 Comments

how do i subscribe to it? i thought that since i'm subscribing to the compressImage and this one is nested in it it will be called automatically. Can you give me more info ?
You probably want to use switchMap instead of map and make sure that the inner observable get's returned from the lambda function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.