Chaining methods, but detecting if called from server


#1

Sometimes I want to chain meteor methods where

  • the initial method comes from the client
  • subsequent methods are called from the server

I want the subsequent methods to not be called from the client e.g.

if (this.connection) {
  throw new Meteor.Error('called-from-client', 'Error')
}

But, if the initial method is called then connection is still defined.

// Client
Meteor.call('firstMethod');

// Server
Meteor.methods({

  firstMethod: function () {
    Meteor.call('serverOnlyMethod');
  },

  serverOnlyMethod: function() {
    // throw error if called from client
  }

})

Has anyone found a way to distinguish?


#2

Why would you want to call a method from the server?

Meteor methods are the way for the client to communicate with the server. So I don’t see a reason why you should do a method call from somewhere in the server.


#3

To allow code to run asynchronously.


#4

@themeteorchef posted a snippet related to this a while back that might help: Preventing Client Calls to a Server-Only Method