maria
maria

Reputation: 159

angular2/4 Observable subscription

Im trying to make a api call to my server database, but its not currenty working.

I have the following error:

enter image description here

    Type 'Subscription' is not assignable to type 'Observable<Comment[]>'.
  Property '_isScalar' is missing in type 'Subscription'.

What im i doing wrong and why?

import { Injectable } from '@angular/core';
import { Http, RequestOptions, URLSearchParams } from '@angular/http';
import {Observable} from 'rxjs/Observable';
import { Comment } from './models/comment'
import 'rxjs/add/operator/map';


@Injectable()
export class CoreService {

  constructor(private http: Http, private URLSearchParams : URLSearchParams) { }

  options = [];
  result = [];

  getUserByName(name: string): Observable<Comment[]> {

    let url = "http://localhost:8080/api";


    return this.http.get(url)
        .map(response => response.json())
        .subscribe(result => this.result = response);
  }

}

comment:

    export class Comment {
    constructor(
        public text:string
    ){}
}

Upvotes: 3

Views: 3943

Answers (1)

eko
eko

Reputation: 40677

I think you are trying to subscribe to getUserByName() inside your component.

If that's the case you should remove subscribe from your service.

You can change it with the do() operator to "cache" your data.

return this.http.get(url)
        .map(response => response.json())
        .do(result => this.result = response);

Upvotes: 4

Related Questions