pawankumar meshram
pawankumar meshram

Reputation: 55

How to return json data from service

How can I get data return from post service as I am geeting subscription always. Even i try to convert in my app component I am not able to it.

facility_sevice.ts

saveFacility(facility) { 
    const headers = new Headers({'Content-Type': 'application/json'});
    return this.http.post('http://localhost:8000/saveFacility', 
facility, headers)
    .subscribe(
    (response: Response) => {
    return response.json();
},
(error) => {
return error.json();
}
);
}

facility_component.ts

savePatient() {
const response = 
this.facilityService.saveFacility(this.facilityForm.value)
console.log(response);
}

I am getting sbscription object but I want to return as json.

Upvotes: 0

Views: 1380

Answers (1)

test_124
test_124

Reputation: 1403

RxJS library provides Observable operators which you can use to manipulate the data being emitted like map, filter.

Try using map operator:

    saveFacility(facility) { 
        const headers = new Headers({'Content-Type': 'application/json'});
        return this.http.post('http://localhost:8000/saveFacility', facility, headers)
            .map(response => response.json())
            .subscribe((response: Response) => {
                    return response.json();
                },
                (error) => {
                    return error.json();
                }
            );
    }

Upvotes: 3

Related Questions