Property 'subscribe' does not exist on type '() => Observable <any>'

service file

import { Observable } from 'rxjs/Rx';
import { Http,Response} from '@angular/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/Map';

@Injectable()
export class VideoService {

   private geturl = '/api/videos';
   constructor(private _http:Http) { }

   getvideos() {
       return this._http.get(this.geturl).map((response:Response) => {
          response.json()
       });
   }
}

      

here's a subscription method showing this error

import { VideoService } from '../video.service';
import { Component, OnInit } from '@angular/core';
import { Video } from '../video';
import { Observable } from 'rxjs/Observable';


@Component({
    selector: 'app-videocenter',
    templateUrl: './videocenter.component.html',
    styleUrls: ['./videocenter.component.css']
})
export class VideocenterComponent implements OnInit {
    videos: any;
    onselectvideo: Video;
    switch: boolean = false

    constructor(private videoserv: VideoService) {
        //console.log(this.videos);
    }
    onselect(vid: any) {
        this.switch = true;
        this.onselectvideo = vid;
        console.log(vid);
    }
    ngOnInit() {
         this.videos = this.videoserv.getvideos .subscribe((response) => {
             this.videos = response;
         });
    }

}

      

I have a service file in which I have to call my API to get the API, and when I am going to subscribe a method in another class where I call this getvideos () service method, then it shows an error that the "subscription" property does not exist for type () => observable

+14


source to share


2 answers


You are not calling the method getVideos

. You are calling subscribe

for a function reference getVideos

, not a return value. Call subscribe

after you call getVideos()

:



ngOnInit() {
    this.videoserv.getvideos().subscribe((response) => {
         this.videos = response
    });
}

      

+53


source


ngOnInit() {
    this.videoserv.getvideos().subscribe((response) => {
         this.videos = response
    });
}

      



You must call a method getvideos()

on the service videoserv

. You missed ()

, getvideos

- it is a method, not a property.

+3


source







All Articles