I have an Angular application in which I'm using the RxJs library to fetch data from a backend API server. The call looks like this:
allPowerPlants(onlyActive: boolean = false, page: number = 1): PowerPlant[] {
const self = this;
const path = `/powerPlants?onlyActive=${onlyActive}&page=${page}`;
this.apiService.get(path).subscribe(
powerplants => {
powerplants.map(item => {
if (this.isPowerPlant(item)) {
// make the item an instance of PowerPlant
self.powerPlants.push(item as PowerPlant);
}
});
},
err => {
console.log('oops **** some error happened');
// handle error
});
return this.powerPlants;
}
I now have the following questions:
Is the way I return the array ideal? In the line where I do:
return this.powerPlants
the powerPlants is an array in the class that is enclosing this method!
What I did was that I just started my Angular app, but the backend API server was not running locally on my machine. When I accessed my app on the browser, I noticed that my app is constantly making calls to the backend API server. How do I avoid this? Why is the app making that many calls where every call was returning a 404! After some time, I could see in my browser console that it had made more than 10000 HTTP calls to the backend API server with each call having returned a 404!
The HTTP method OPTIONS is getting executed instead of a GET! Why is this? I understand that this has got to do something with the CORS filter, which I have already enabled on my backend API server!