Search code examples
javascriptangularangular2-http

Angular 2 Http get not triggering


As said in the title, nothing is happening when I subscribe to my observable. There is no error in the console or during the build. Here is my code :

My service

  getBlueCollars(): Observable<BlueCollar[]> {
return this.http.get(this.defaultAPIURL + 'bluecollar?limit=25').map(
  (res: Response) => {
    return res.json();
  });
}

My component

ngOnInit() {
    this.planifRequestService.getBlueCollars().subscribe(
      data => {
        this.blueCollars = data;
        console.log('Inner Blue Collars', this.blueCollars);
      },
      err => console.log(err)
    );
    console.log('Value BlueCollars : ', this.blueCollars);
}

So the second console.log is triggering with "Value BlueCollars : Undefined", and the log in my subscribe is never showed. As well, I can't see the request sent in the Networt tab of Chrome.

So I tried to simplify everything with the following code :

let response: any;
this.http.get('myUrl').subscribe(data => response = data);
console.log('TestRep: ', response);

Same problem here, no error, response is undefined. It seems the subscribe is not triggering the observable. (The URL is correct, it is working on my swagger or with postman.)

I'm on Angular 2.4.9

Edit

So I tried to copy/past the code of my request on a brand new project, everything is working fine. The request is triggered and I can get the JSON response correctly. So there is something maybe on the configuration of my project that is forbiding the request to trigger correctly.


Solution

  • Ok just found what was going on. I am using a fake backend in order to try my login connexions that is supposed to catch only specified URL. However for wathever raison it was catching all the requests, so that explain everything. Thx for your help everybody.