5

I am trying to perform a get request to my api on localhost from my angular app. But for some reason the angular http client seems not to perform the get request.

I can see on my api server that no get request was made. And this is causing the error. The api is working fine I tried doing a curl and I get the expected results.

EXCEPTION: Uncaught (in promise): TypeError: Cannot read property 'subscribe' of undefined

api.service

import { Injectable } from '@angular/core';
import { Headers, Http, Response, URLSearchParams, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Rx';

@Injectable()
export class ApiService {
    headers: Headers = new Headers({
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    });

    api_url: string = 'http://localhost:5001';

    constructor(private http: Http) { }

    private getJson(response: Response) {
        return response.json().results;
    }

    checkForError(response: Response): Response {
        if (response.status >= 200 && response.status < 300) {
            return response;
        } else {
            var error = new Error(response.statusText);
            error['response'] = response;
            Observable.throw(error);
        }
    }

    get(path: string, params: URLSearchParams): Observable<any> {
        return this.http
            .get(`${this.api_url}${path}/`, { headers: this.headers })
            .map(this.checkForError)
            .catch(err => Observable.throw(err))
            .map(this.getJson);
    }

api.component.ts

import { Component, OnInit } from "@angular/core";
import { ApiService } from './api.service';

@Component({
    selector: 'item',
    templateUrl: './item.component.html'
})
export class ItemComponent implements OnInit {
    private itemData;
    private errorAlert;

    constructor(private apiService: ApiService) {}

      ngOnInit() {
          this.apiService.get('/items').subscribe(
              result => this.itemData = result,
              error => this.errorAlert = {msg: error, type: 'danger', closable: true},
          );
      }
}

Attached the console enter image description here enter image description here

28
  • In checkForError there should be a return before Observable.throw(error) but that seems unrelated to your error message. map(getJson) should be map(this.getJson). Commented Oct 4, 2016 at 12:30
  • 1
    The observable doesn't do anything before subscribe() is called. Because subscribe() fails there can't be a request. Commented Oct 4, 2016 at 12:31
  • Provide some screenshot from network tab of dev tools Commented Oct 4, 2016 at 12:34
  • @GünterZöchbauer anyway the get request should start Commented Oct 4, 2016 at 12:36
  • 1
    Posted the console error trace to the question. Commented Oct 4, 2016 at 12:44

1 Answer 1

3

Maybe you should try to backtrack (or forward-track) the problem:

start with:

 this.http.get('/my/hardcoded/url').subscribe();

The problem might be in the headers or in the interpolated url or anywhere.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.