I'm new to angular 2 and I'm trying to make a REST GET request, but I'm getting this error when trying:
error TS2339: Property 'then' does not exist on type 'Observable<Connection[]>'.
Here is the component calling the service:
import { Component, OnInit} from '@angular/core';
import { Router } from '@angular/router';
import { Connection } from './connection';
import { ConnectionService } from './connection.service';
let $: any = require('../scripts/jquery-2.2.3.min.js');
@Component({
selector: 'connections',
styleUrls: [ 'connections.component.css' ],
templateUrl: 'connections.component.html',
providers: [ConnectionService]
})
export class ConnectionsComponent implements OnInit {
connections: Connection[];
selectedConnection: Connection;
constructor(
private connectionService: ConnectionService,
private router: Router) { }
getConnections(): void {
this.connectionService.getConnections().then(connections => {
this.connections = connections;
});
}
ngOnInit(): void {
this.getConnections();
}
onSelect(connection: Connection): void {
this.selectedConnection = connection;
}
gotoDetail(): void {
this.router.navigate(['/connectiondetail', this.selectedConnection.id]);
}
}
Here is the ConnectionService:
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Connection } from './connection';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class ConnectionService {
private connectionsUrl = 'https://localhost/api/connections'; // URL to web API
constructor (private http: Http) {}
getConnections(): Observable<Connection[]> {
return this.http.get(this.connectionsUrl)
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body.data || { };
}
private handleError (error: Response | any) {
// In a real world app, we might use a remote logging infrastructure
let errMsg: string;
if (error instanceof Response) {
const body = error.json() || '';
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return Observable.throw(errMsg);
}
}
How do I fix this error?
Thank you,
Tom