0

I tried everything and I cannot get an http request to go out to my node server on heroku. I can hit the route manually so its not the server. I will paste my service and my page.

**Class is subscription.service.ts

import {Http, Response} from '@angular/http'
import {Injectable} from '@angular/core'
import 'rxjs/add/operator/map';

@Injectable()
export class SubscriptionService {

  http:Http;
  constructor(http:Http){
    this.http = http;
  }

  getEntries() {
    return this.http.get('my url here *****').map(res => res.json());
  }
}

**Class is dashboard.component.ts

import {Component, ViewEncapsulation} from '@angular/core';
import {SubscriptionService} from '../../_services/subscription.service';
import 'rxjs/Rx';

@Component({
  selector: 'dashboard',
  providers: [SubscriptionService],
  encapsulation: ViewEncapsulation.None,
  styles: [require('./dashboard.scss')],
  template: require('./dashboard.html')
})
export class Dashboard {
  getData: string;

  constructor(private subscriptionService: SubscriptionService) {
  }

  ngOnInit() {
    console.log("test!!");
    this.subscriptionService.getEntries()
      .subscribe(data => this.getData = JSON.stringify(data),
        error => console.log("Error!"),
        () => console.log("finished!")
      );
  }
}

My ngOnInit() is being called, I see the console print, but no request shows up in logs on heroku. Also no errors show up in console.

2
  • Go to the "Sources" tab of the Chrome inspector and turn on "Pause on exceptions" and turn on "pause on uncaught exceptions." Then re-run your code. Are any errors caught? Commented Nov 6, 2016 at 4:45
  • Nothing showed up. Commented Nov 6, 2016 at 4:51

1 Answer 1

1

Make sure you have imported the HttpModule in root. I don't see anything else which can cause this. For make sure http is working you can put a break point in SubscriptionService on getEntries method and follow where it leads you.

Update:- as pointed out by @peeskillet there is nothing wrong with your dependency. try to debug and update your question with more information.

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

4 Comments

What you are showing is functionally equivalent. Adding the access modifier is just shortand for assigning it to a class property. Without the access modifier, no property gets added to the class, and the constructor arg is onlt available in the constructor
in that case shouldn't this http:Http; constructor(http:Http){ this.http = http; } be http:Http; constructor(http){ this.http = http; }?
No. The Http (in constructor) is required as the token for injection. The class http: Http is only specifying the type. You could leave the type off and it would be the same (the type would be implied)
So I am silly, and didn't realize I needed httpModule in my module class for the component, and couldn't just have it on the app module. Thank you for your help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.