[SOLVED] Events publish property undefined

Hi there !
I’m trying to change my view subscribing to events but I got an error :
EXCEPTION: TypeError: Cannot read property ‘publish’ of undefined
when doing : "this.events.publish(‘someText’);"
I’ve imported Events from ‘ionic-angular’ and injected Events in constructor.

Not sure what I’m missing here :confused:

If someone could help me :slight_smile:

Thanks everyone and sorry for being a beginner !!

EDIT: here is the code of my Service :
This is a simple authentication service,

the error is in authenticate function , in publishing at the events

import {Injectable, Inject} from ‘@angular/core’;
import {Http, Headers} from ‘@angular/http’;
import {Events} from ‘ionic-angular’;

@Injectable()
export class AuthService {

http;
isLoggedin: boolean;
AuthToken;

static get parameters() {
    return [Http];
}

constructor(http, private events: Events) {
    this.http = http;
    this.isLoggedin = false;
    this.AuthToken = null;
}

/* logged */
isLoggedIn() {
    return this.isLoggedin;
}

/* username */
storeUserName(username) {
    window.localStorage.setItem('demAppUserName', username);
}

loadUserName() {
    return window.localStorage.getItem('demAppUserName');
}

/* usermode */
storeUserMode(usermode) {
    window.localStorage.setItem('demAppUserMode', usermode);
}

loadUserMode() {
    return window.localStorage.getItem('demAppUserMode');
}

/* user creds */
storeUserCredentials(token) {
    window.localStorage.setItem('demAppToken', token);
    this.useCredentials(token);   
}

useCredentials(token) {
    this.isLoggedin = true;
    this.AuthToken = token;
}

loadUserCredentials() {
    var token = window.localStorage.getItem('demAppToken');
    this.useCredentials(token);
}

destroyUserCredentials() {
    this.isLoggedin = false;
    this.AuthToken = null;
    window.localStorage.clear();
}

/* auth */
authenticate(user) {
    var creds = "name=" + user.name + "&password=" + user.password;
    var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    
    return new Promise(resolve => {
        this.http.post('http://localhost:3000/authenticate', creds, {headers: headers}).subscribe(data => {
            if(data.json().success){
                this.storeUserName(user.name);
                this.storeUserMode(data.json().mode);
                this.storeUserCredentials(data.json().token);
                this.events.publish('user:login');
                resolve(true);
            }
            else
                resolve(false);
        });
    });
}

adduser(user) {
    var creds = "name=" + user.name + "&password=" + user.password + "&firstname=" + user.firstname + "&lastname=" + user.lastname;
    var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    
    return new Promise(resolve => {
        this.http.post('http://localhost:3000/adduser', creds, {headers: headers}).subscribe(data => {
            if(data.json().success){
                this.storeUserName(user.name);
                this.storeUserMode(data.json().mode);
                this.events.publish('user:signup');
                resolve(true);
            }
            else
                resolve(false);
        });
    });
}

getinfo() {
    return new Promise(resolve => {
        var headers = new Headers();
        this.loadUserCredentials();
        console.log(this.AuthToken);
        headers.append('Authorization', 'Bearer ' +this.AuthToken);
        this.http.get('http://localhost:3000/getinfo', {headers: headers}).subscribe(data => {
            if(data.json().success)
                resolve(data.json());
            else
                resolve(false);
        });
    })
}

postCanvas(canvas) {
    return new Promise(resolve => {
        var headers = new Headers();
        this.loadUserCredentials();
        console.log(this.AuthToken);
        headers.append('Authorization', 'Bearer ' + this.AuthToken);
        this.http.get('http://localhost:3000/postCanvas', {headers: headers, image: canvas}).subscribe(data => {
            if(data.json().success)
                resolve(data.json());
            else
                resolve(false);
        });
    })
}

logout() {
    this.destroyUserCredentials();
    this.events.publish('user:logout');
}

getClients() {

    var data = "username=" + this.loadUserName();

    return new Promise(resolve => {
        var headers = new Headers();
        this.loadUserCredentials();
        console.log(this.AuthToken);
        headers.append('Authorization', 'Bearer ' + this.AuthToken);
        this.http.get('http://localhost:3000/getClients', data, {headers: headers}).subscribe(data => {
            if(data.json().success)
                resolve(data.json());
            else
                resolve(false);
        });
    })
}

addPatient(patient) {
    console.log('addPatient : ' + patient.name + ' with manager : ' + this.loadUserName());
    var creds = "name=" + patient.name + "&password=" + patient.password + "&firstname=" + patient.firstname + "&lastname=" + patient.lastname + "&manager=" + this.loadUserName();
    var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    
    return new Promise(resolve => {
        this.http.post('http://localhost:3000/addPatient', creds, {headers: headers}).subscribe(data => {
            if(data.json().success){
                resolve(true);
            }
            else
                resolve(false);
        });
    });
}

}

Thanks a lot for helping :slight_smile: !!

It would help to see the actual complete code of the class. I suspect you aren’t actually initializing what you think you are.

You’re mashing up TypeScript and JavaScript. You need to pick one (and TypeScript is the right choice).

ok, I did mixed both typescript and javascript in dependency injections, which did this error, at least I will carry on writing only javascript !
thanks !

For anybody else, I had the same issue, I removed

static get parameters() {
        return [Http];
    }

and that did the trick in my case…

1 Like