Possible bug: Events.publish returns Array, not Object


#1

Hello, Ive got the following piece of code, which is called when I log in through Firebase.

// authProvider.ts
setLoggedIn(user) {
    console.log('set logged in');
    this.email = user.email;
    this.firstName = user.firstName;
    this.profileImageURL = user.profileImageURL;

    this.events.publish('user:loggedin', user);
  }

Now in my MainPage I subscribe like this:

this.events.subscribe('user:loggedin', (user) => {
        console.log('user:loggedin event', user);
        this.firstName = user.firstName;
        this.email = user.email;
        this.profileImageURL = user.profileImageURL;
      });

which turns out to set everything to undefined.
The console.log output shows that user is not an Object, but an array of 1 object.

Is this expected behavior? Of course I know the solution is to just put user[0].email, but the documentation does not specify an array. Thought I’d let you know.

Still a great product, cheers guys.


#2

I guess it’s array so you could potentially send multiple objects or values


#3

I think it is the exact opposite. LOL.

Here’s the code to Subscribe

this.events.subscribe('save-settings', (args) => {

      let settingsArray  = args as  UserSettings[];
      console.log("settingsArray length is " + settingsArray.length); **---> 1 only??**
      this.userSettings = settingsArray[0]; //should have a value because it was just saved
      this.oldUserSettings = settingsArray[1]; 
}

Here’s my code to publish:

  let settingsArray : UserSettings[] = [this.userSettings, this.oldUserSettings == null? this.userSettings: this.oldUserSettings];

   //publish(topic: string, ...args: any[]): any[];
    this.events.publish('save-settings', settingsArray);
    console.log("settingsArray length is " + settingsArray.length); **---> sending 2 objects **

I’m pushing an array with 2 objects and only receiving an array with length 1. This looks like a bug to me.

Here’s my build version:

Your system information:

Cordova CLI: 6.3.1
Gulp version:  CLI version 3.9.1
Gulp local:
Ionic Framework Version: 2.0.0-rc.3
Ionic CLI Version: 2.1.0
Ionic App Lib Version: 2.1.0-beta.1
OS: Windows 7 SP1
Node Version: v6.7.0

#4

After reading Events doc needs clarification for .publish() #544 I found a way around this.

The subscription gets the first item out of the array which is also an array.

//so retarded
      let settingsArray  = args[0] as  UserSettings[];
      console.log("args is " + settingsArray.length);