Alex D
Alex D

Reputation: 828

Angular 4 Property 'body' does not exist on type 'Response'

I Get the following error when I try to get a csv from a web api call. When hitting that URL without angular it downloads the csv.

Property 'body' does not exist on type 'Response'.

Service

getViolationFile(mid: string, cp: string, d: string, pl: string) {
    let params: URLSearchParams = new URLSearchParams();
    params.set('mid', mid);
    params.set('cp', cp);
    params.set('d', d);
    params.set('pl', pl);

    let headers = new Headers({
      'Accept': 'text/csv'
    });

    let options = new RequestOptions({
      headers: headers,
      search: params,
    });

    return this.http.get('MappViolations/Download?', options);
  }

Component

public submit() {
    this.incomingMappSummaryService.getViolationFile(this.mid, this.cp, this.domain, this.pl)
      .subscribe(data => this.downloadFile(data.body), //gets error here 
      error => console.log('error'),
      () => console.log('success'));
  }



downloadFile(data: Response) {
    let contentType = 'text/csv';
    let blob = new Blob([data.body], { type: contentType }); //this might not be right either but I dont get an error for it
    saveAs(blob, 'data.csv');
    let url = window.URL.createObjectURL(blob);
    window.open(url);
  }

Upvotes: 1

Views: 2921

Answers (3)

CREM
CREM

Reputation: 1991

Try this on your Service:

return this.http.post(endPointApi, body, options)
.map((data) => { return data.text(); })
.catch(error => { console.log(error)});

Upvotes: 1

Michael Desigaud
Michael Desigaud

Reputation: 2135

The type of property data is already Response so no need to use data.body, just :

this.downloadFile(data)

Upvotes: 0

jacobedawson
jacobedawson

Reputation: 3202

Try using bracket notation: data['body']

Otherwise double check you're receiving the correct response in JSON format:

data.json()

Upvotes: 3

Related Questions