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.
HttpModulein app.module or main.module file???return x;instead ofreturn null;and in component you can subscribe to login method...