0

i found this great post Creating and returning Observable from Angular 2 Service and now I try to do the same, but get the this error:

core.umd.js:2838 EXCEPTION: Cannot read property 'next' of undefined

here is my service:

@Injectable()
export class ReadBuchungenService {

    public activeProject : ReplaySubject<any>;
    private get_all_buchungen_url : string = "xxxxxxx/xxx/";


    constructor (private http : Http) {}

    public load(timestamp : number) {
        let params = new URLSearchParams();
        params.set('timestamp', String(timestamp));

        this.http.get(this.get_all_buchungen_url, { search: params })
            .subscribe(res => this.activeProject.next(res));
        return this.activeProject;
    }
}

compiling in es6 and here are the dependencies from the package.json:

"dependencies": {
    "@angular/common": "^2.2.4",
    "@angular/compiler": "^2.1.2",
    "@angular/core": "^2.2.4",
    "@angular/http": "^2.1.4",
    "@angular/platform-browser": "^2.1.2",
    "@angular/platform-browser-dynamic": "^2.1.2",
    "@angular/router": "^3.1.4",
    "core-js": "^2.4.1",
    "reflect-metadata": "^0.1.8",
    "rxjs": "5.0.0-beta.12",
    "systemjs": "^0.19.40",
    "zone.js": "^0.6.26"
  }

Thanks!

ok now i tried to set the ReplaySubject Object width:

public activeProject : ReplaySubject<any> = new ReplaySubject(1);

but then i get a Unexpected token < error, also after a fresh installation of angular.

Image of the complete error:

IMAGE

Community
  • 1
  • 1
miholzi
  • 922
  • 1
  • 14
  • 36

3 Answers3

1

Your activeProject is not set. Try to define it like:

public activeProject:ReplaySubject<any> = new ReplaySubject(1);
Petr Adam
  • 1,425
  • 11
  • 23
  • the same error as commented in "Shai" answer when i set it. "Error: (SystemJS) Unexpected token <", i copied from here, is it maybe a compiling error? – miholzi Dec 06 '16 at 15:26
  • You can get more info if you click to expand (...) in that error message. It could show the file with unexpected < in, so you can track it down to determine the reason - most likely, HTML was returned instead of the javascript due to some error in the config – Petr Adam Dec 06 '16 at 15:33
  • sure: (index):16 Error: (SystemJS) Unexpected token <(…) (anonymous function) @ (index):16 ZoneDelegate.invoke @ zone.js:232 Zone.run @ zone.js:114 (anonymous function) @ zone.js:502 ZoneDelegate.invokeTask @ zone.js:265 Zone.runTask @ zone.js:154drainMicroTaskQueue @ zone.js:401ZoneTask.invoke @ zone.js:339 – miholzi Dec 06 '16 at 15:34
  • this doesn't help much, aren't there more three dots to click, to get deeper exeption? http://i1.kym-cdn.com/photos/images/facebook/000/531/557/a88.jpg – Petr Adam Dec 06 '16 at 15:38
  • :-) here a screenshot https://s11.postimg.org/d3lpqxdpv/screen_shot.jpg – miholzi Dec 06 '16 at 15:45
  • Cannot this http://stackoverflow.com/questions/34387174/angular-2-quickstart-unexpected-token help you somehow? I guess the error is somewhere else then in code you posted here – Petr Adam Dec 06 '16 at 15:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129928/discussion-between-miholzi-and-petr-adam). – miholzi Dec 06 '16 at 16:01
1

Now i found the mistake, it was the import statement from the ReplaySubject class.

Wrong: import { Observable, ReplaySubject } from "rxjs";

Right: import { ReplaySubject } from 'rxjs/ReplaySubject';

Here the complete code of the service:

import  {Inject, Injectable} from '@angular/core';
import  { Http, URLSearchParams } from '@angular/http';
import 'rxjs/Rx';
import { ReplaySubject } from 'rxjs/ReplaySubject';


@Injectable()
export class MyService {

    public activeProject: ReplaySubject<any> = new ReplaySubject(1);
    private get_all_buchungen_url: string = "xxxx/xxx";

    constructor(private http: Http) {
    }

    public load(timestamp: number) {

        let params = new URLSearchParams();
        params.set('timestamp', String(timestamp));

        this.http.get(this.get_all_buchungen_url, {search: params})
            .subscribe(res => this.activeProject.next(res));
        console.log(this.activeProject)
        return this.activeProject;
    }
}
miholzi
  • 922
  • 1
  • 14
  • 36
0

You didn't initialize activeProject. Try:

public activeProject : ReplaySubject<any> = new ReplaySubject<any>(1);
Shai
  • 3,659
  • 2
  • 13
  • 26