TypeError: Cannot read property 'publish' of undefined

every time this throwErrorObservable is called i get undefined error (due to the events objects which is undefined)
I cannot understand why, what is the reason ? can i inject the Events object in my service provider ?
Thx fo any answer, i m stuck with this…

export class ErrorHandlerService {

constructor( private events: Events ) {
console.log(“constructor ErrorHandlerService events”);
console.log(events); //this is well defined :slight_smile:

public throwErrorObservable(error: any): any {
console.log(this.events) //this is “undefined” when the function is called WHY???
this.events.publish(‘http:status’, error.status, Date.now());
return Observable.throw(new Error(error.status));




providers: [

export class AppModule { }

Please edit your post and use the </> button above the post input field to format your code or error message or wrap it in ``` (“code fences”) manually. This will make sure your text is readable and if it recognizes the programming language it also automatically adds code syntax highlighting. Thanks.

I’m going to guess you’re losing execution context because you’re passing raw methods as callbacks. Don’t do that. Always use closures instead.

I am doing the same, i want to publish one event from catch error to my app component then subscribe it if there is error then set dfiferent page.

You will say do it in when you are subscribing that http call but i have 40+ api call i have to do it manually on every page.

I am getting this

ERROR TypeError: Cannot read property 'publish' of undefined
    at VM4271 main.js:967
    at t.invokeTask (VM4269 polyfills.js:3)
    at Object.onInvokeTask (VM4270 vendor.js:5125)
    at t.invokeTask (VM4269 polyfills.js:3)
    at r.runTask (VM4269 polyfills.js:3)
    at e.invokeTask (VM4269 polyfills.js:3)
    at i.isUsingGlobalCallback.invoke (VM4269 polyfills.js:3)
    at n (VM4269 polyfills.js:3)

this is in api catch error


on constructor of app component

this.events.subscribe('user',()=> {
        console.log("event called")