Fred J.
Fred J.

Reputation: 6029

this.userId undefined in method called via DDP

This Meteor server failed to fetch the userId inside a method which has been called from a remote DDP.call. How can I get the userId who called the method from a remote DDP? thx

//app 1 server
let app2_Conn = DDP.connect('http://localhost:4000');
Meteor.methods ({
  'callOut': () => {
    app2_Conn.call('app2_method', args);
  }
});

//app 2 server
Meteor.methods ({
  'app2_method': () => {
    const id = Meteor.userId(); //null
    const iD = this.userId;     //undefined
  }
});

Upvotes: 0

Views: 121

Answers (1)

Sean
Sean

Reputation: 2729

It's because you are using an arrow function. Arrow functions change the way the binding of this works.

Change to:

Meteor.methods({
  'app2_method'() {
    const id = this.userId;
  }
});

Upvotes: 2

Related Questions