3

I am having a really hard time determining what is wrong with my code. I am trying to create a service class to connect to my separate database application which exposes an open (unsecure) REST API.

If I connect to the address in my browser I can correctly see the results, and a connection is logged on my server. However when I try to connect via my Angular2 service class I get no errors and no connection attempt on my server.

I have been rewriting the below over and over again, reading the docs and cannot figure it out! I am using 2.0.1.

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

@Injectable()
export class AuthenticationService {

  private loggedIn = false;
  private loginUrl = 'http://localhost:3000/api/users';

  constructor(private http: Http) { }

  login(user:any): Observable<any[]> {

    var x = this.http.get(this.loginUrl)
                    .map(this.extractData)
                    .catch(this.handleError);

    console.log(x);
    console.log("Sent?");

    return null;
  }

  private handleError (error: any) {
    let errMsg = (error.message) ? error.message :
      error.status ? `${error.status} - ${error.statusText}` : 'Server error';
    console.error(errMsg); // log to console instead
    return Observable.throw(errMsg);
  }

  private extractData(res: Response) {
    let body = res.json();
    return body.data || { };
  }
}

In my console I see the results of console(x) and console('sent?') so I know the code is being executed.

2
  • Have you injected HttpModule in app.module or main.module file??? Commented Oct 17, 2016 at 17:07
  • You can use return x; instead of return null; and in component you can subscribe to login method... Commented Oct 17, 2016 at 17:10

2 Answers 2

1

Without subscribe() or toPromise() an Observable isn't doing anything because they are lazy

var x = this.http.get(this.loginUrl)
                .map(this.extractData)
                .catch(this.handleError);

This way it makes the request

var x = this.http.get(this.loginUrl)
                .subscribe(this.extractData, this.handleError);

You need to be cautious about using thin. in extractData and handleError. It's usually better to always use arrow functions

var x = this.http.get(this.loginUrl)
                .subscribe(data => this.extractData(data), 
                           err => this.handleError(err));
Sign up to request clarification or add additional context in comments.

1 Comment

Ah awesome thanks! Will accept as soon as I am allowed.
1

Your forgot to subscribe to the http.get call.

You need to type:

 http.get(...).subscribe(data => console.log(data)); 

to make the actual call.

Observables are cold, that means without subscribing nothing happens.

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.