Search code examples
meteorddp

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
  }
});

Solution

  • 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;
      }
    });