title: Handling 204- No Content response in HTTP Observable - Angular2
body: <p>In my Angular2 application, I am taking responses from a REST API and manipulating them in order to display them in the user interface. </p>
  
  <p>I coded my application according to following tutorial.</p>
  
  <p><a href="https://www.barbarianmeetscoding.com/blog/2016/04/02/getting-started-with-angular-2-step-by-step-6-consuming-real-data-with-http/" rel="noreferrer">https://www.barbarianmeetscoding.com/blog/2016/04/02/getting-started-with-angular-2-step-by-step-6-consuming-real-data-with-http/</a></p>
  
  <p><em>This is my method to get the response from server</em></p>
  
  <pre><code>getTendersByCategory(categoryName: string){
  
          console.log("get tenders by category "+categoryName);
  
          let tendersByCategory = this._http
              .get(this._endpointUrl + "tender/get-by-category-name/" + categoryName)
              .map(mapTenders)
              .catch(handleError);
  
          return tendersByCategory;
  
  }
  </code></pre>
  
  <p>Following functions are used to manipulate the response</p>
  
  <pre><code>function mapTenders(response: Response): Tender[] {
      console.log('tender.service.ts -&gt; mapTenders');
  
      /*if(response.status == 204){
          console.log("response = 204 - No content in response");
          return null;
      }*/
  
      return response.json().map(toTender);
  }
  
  
  function toTender(r: any): Tender {
      console.log('tender.service.ts -&gt; toTender');
      let tender = &lt;Tender&gt;({
          id: r.id,
          name: r.name,
          publisher: r.publisher,
      });
      return tender;
  }
  
  function handleError(error: any) {
      console.error(error.message);
      return Observable.throw(error.message);
  }
  </code></pre>
  
  <p>The problem now is, I get 204 - No content response for my requests. In those cases, I get the error </p>
  
  <blockquote>
    <p>Cannot read property 'map' of null</p>
  </blockquote>
  
  <p>What I have tried up to now is, I check whether the response is 204 in my <code>mapTenders</code> function; if so I return null without proceeding with mapping to my object. (This part of code has been commented out in the given code)</p>
  
  <p>Is that the correct way of handing this response? </p>
  
  <p>But in that case, even though I get rid of the error, in my <code>.component.ts</code> file I do not get an empty list of tenders. It shows all the tenders without any sort of filtering. (That may be the result from refreshing the entire page, but apparently, page does not reload.)</p>
  
  <p>This is the method in my component.ts file.</p>
  
  <pre><code>getTendersByCategory(categoryName: string){
  
          this._tendersService.getTendersByCategory(categoryName).
              subscribe(
  
              tenders =&gt; this._tenders = tenders,
              error =&gt; this._error_tender = error
  
              );
  
         console.log("tenders.component.ts.getTendersByCategory -&gt; tenders array = "+this._tenders.length);
  
         //for debugging purpose
         //this prints all the tenders. What I wished to see is nothing getting printed
         for(let i=0; i&lt; this._tenders.length;i++){
             printTender(this._tenders[i]);
         }
  
      }
  </code></pre>
  
  <p>What can I do here? How should I handle 204 response when using observables ?</p>