I'm making GET requests to an Express REST API, and returning data after setting res.status to 200. In Angular the response contains the data, but response.status is always undefined.
[EDIT START]
Following from Martin Adámek's answer below, I tried changing my get request to use an Observable rather than a Promise, and to include { observe: 'response' } in the request. But the response was exact the same - still no response.status.
Angular 4 get (with Observable)
requestNew(endPoint:string, type:string, query:string, data:any) {
this.http[type](this.configService.apiHost + endPoint + query, data ? data : '', { observe: 'response' })
.subscribe(
(response:HttpResponse<any>) => {
console.log(response) // Array containing expected data
console.log(response.status) // undefined
}
)
}
[EDIT END]
Angular 4 get
import { HttpClient, HttpResponse, HttpErrorResponse } from '@angular/common/http';
request(endPoint:string, type:string, query:string, data:any) {
let promise = new Promise((resolve, reject) => {
this.http[type](this.configService.apiHost + endPoint + query, data ? data : '')
.toPromise()
.then((response: HttpResponse<any>) => {
resolve(response);
})
.catch((error: HttpErrorResponse) => {
})
});
return promise;
}
Express API:
router.get('/clients', (req, res) => {
clientService.GetClients()
.then((data) => {
res.status(204).json(data); // data is sent
})
})
How can I access response.status in Angular 4?
{observe: 'response'}as third parameter, but forget, it should be second parameter, as there is nopayload.body.