Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular2/4 Observable subscription

Tags:

angular

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
    ){}
}
like image 225
maria Avatar asked May 23 '26 21:05

maria


1 Answers

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);
like image 80
eko Avatar answered May 25 '26 11:05

eko