简体   繁体   中英

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:

在此输入图像描述

    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
    ){}
}

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM