Search code examples
meteorangular-meteor

Sending array between server and client via Meteor.methods using angular-meteor


I'm trying to get an array to be managed by my server and shared across to my client. I figured that the Meteor.methods() would be the way to do it by creating a getter method. Though for some reason it prints to console correctly by the server but returns undefined to the client when I do the Meteor.call() I get the undefined returns regardless of whether the if is true or false, It's always undefined.

///server
Meteor.methods({
  createNewUser: this.createNewUser,
  getRoles: this.getRoles
});
...
getRoles(id) { 
  console.log(roles);
  if (Roles.userIsInRole(id, "admin")) {
    return roles;
  } else {
    return 'blah';
  }
}

///client
ngOnInit() {
 MeteorObservable.autorun().subscribe(() => {
 ...

    Meteor.call("getRoles", Meteor.userId(), (data) => {
      console.log("data", data);
      this.roles = data;
      console.log("roles", this.roles);
    });

    console.log("user list", this.userList);
    console.log("roles", this.roles);
....
});

The other method in the Meteor.methods() works perfectly and I can't seem to work out why this one doesn't seeing as it does so much less.

///output
demo.component.ts:53 user list []
demo.component.ts:54 roles []
demo.component.ts:53 user list [Object]
demo.component.ts:54 roles []
demo.component.ts:48 data undefined
demo.component.ts:50 roles undefined
demo.component.ts:48 data undefined
demo.component.ts:50 roles undefined
demo.component.ts:53 user list [Object, Object, Object, Object, Object, Object]
demo.component.ts:54 roles undefined
demo.component.ts:48 data undefined
demo.component.ts:50 roles undefined

Thanks for any help.


Solution

  • Turns out that my problem was in the Method.call()

    It returns an error and a result, rather than a single object.

        ///client
        Meteor.call("getRoles", Meteor.userId(), (error, data) => {
          if (!error) {
            console.log("data", data);
            this.roles = data;
            console.log("roles", this.roles);
          } else {
            console.log("error: ", error);
          }
        });
    
        ///output
    demo.component.ts:57 user list []
    demo.component.ts:58 roles []
    demo.component.ts:57 user list [Object]
    demo.component.ts:58 roles []
    demo.component.ts:49 data ["admin","spectator","coach","player"]
    demo.component.ts:51 roles ["admin","spectator","coach","player"]
    demo.component.ts:49 data ["admin","spectator","coach","player"]
    demo.component.ts:51 roles ["admin","spectator","coach","player"]
    demo.component.ts:57 user list [Object, Object, Object, Object, Object, Object]
    demo.component.ts:58 roles ["admin","spectator","coach","player"]
    demo.component.ts:49 data ["admin","spectator","coach","player"]
    demo.component.ts:51 roles ["admin","spectator","coach","player"]
    

    This is what I'm expecting.(though not sure why the MeteorObservable is triggered so many times.)