Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'subscribe' does not exist on type 'void'. Angular 2 asp.net core

I get the following error when i try to use subscribe. I had the same issue with .map, but i solved this with replacing the following file https://raw.githubusercontent.com/Microsoft/TypeScript/Fix8518/lib/typescriptServices.js

I've updated Typescript to 1.8.6 and updated visual studio 2015 to update 3.

I have no idea how to fix this issue or if i'm doing something wrong, I've also added

import 'rxjs/Rx'; 

to the bootstrap and app.component classes and still same error.

service code:

import { Injectable } from 'angular2/core';
import {Http, URLSearchParams} from "angular2/http";
import 'rxjs/add/operator/map';

@Injectable()
export class MediaItemService {

constructor(public http: Http) {
    this.http = http;
}

//functions
get() {
   // return this.mediaItems;
    this.http.get("/api/MediaItem").map(response => { response.json() });
}
}

class where service is used:

import {Component, Inject} from 'angular2/core';
import 'rxjs/Rx'; 
import {MediaItemComponent} from './media-item.component';
import {CategoryListPipe} from './category-list.pipe';
import {MediaItemService} from './media-item.service';

@Component({
selector: 'media-item-list',
directives: [MediaItemComponent],
pipes: [CategoryListPipe],
providers: [MediaItemService],
templateUrl: 'app/media-item-list.component.html',
styleUrls: ['app/media-item-list.component.css']
})
export class MediaItemListComponent {


constructor(private mediaItemService: MediaItemService) {

        //.subscribe(mediaItems => {
        //    this.mediaItems = mediaItems;
        //});
}

ngOnInit() {
    this.mediaItems = this.mediaItemService.get().subscribe(mediaItems => {
            this.mediaItems = mediaItems;
        });
}

onMediaItemDeleted(mediaItem) {

    this.mediaItemService.delete(mediaItem);
}

mediaItems; 
}

package.json file:

{
"version": "1.0.0",
"name": "TestFunWithAngi",
"private": true,
"dependencies": {
"angular2": "^2.0.0-beta.17",
"systemjs": "^0.19.31",
"es6-promise": "^3.2.1",
"es6-shim": "^0.35.1",
"reflect-metadata": "^0.1.3",
"rxjs": "^5.0.0-beta.9",
"tsd": "^0.6.5",
"zone.js": "^0.6.12"
},
"devDependencies": {
"typescript": "^1.8.10",
"typings": "^0.8.1",
"grunt": "1.0.1",
"grunt-contrib-copy": "1.0.0",
"grunt-contrib-uglify": "1.0.1",
"grunt-contrib-watch": "1.0.0",
"grunt-ts": "5.5.1"

}
}
like image 718
user5049376 Avatar asked Jun 23 '16 11:06

user5049376


2 Answers

You forgot to return in your get(). Also don't encapsulate the body of lambda functions without returning something. An implicit return is happening if you just do response => response.json(). Otherwise you have to write it like this: response => { return response.json(); }.

Here is an improved version of your method:

get() {
   // return this.mediaItems;
   return this.http.get("/api/MediaItem").map(response => response.json());
}

Also just leave out the this.http = http; in your constructor. Writing public or private in front of the constructor argument already adds it as a class member.

like image 157
rinukkusu Avatar answered Nov 12 '22 00:11

rinukkusu


Your get()-Function does not return the Observable -> nothing to subscribe. Try:

get() {
  return this.http.get("/api/MediaItem").map(response => { response.json() });
}
like image 34
bene Avatar answered Nov 12 '22 00:11

bene