kerosene
kerosene

Reputation: 891

Meteor: define server-functions that are available for methods

What I'm trying to do is the following:

function doSomething(query){
//code
}

Meteor.methods({

    methodOne: function() {
       var res = doSomething("1");
       //some stuff
       return res;
    },
  methodTwo: function() {
       return doSomething("2");
    }
});

If this all is written in the same file everything is okay. But if the function is defined in a different file, the method will end with an exception of a missing function. My aim is to define this function in a Meteor package and call it from another package with an method. The function itself should be not accessible for clients, only by given calls - so the methods are the interface for the user.

Any ideas how to solve that? Maybe with JS object with functions bound, that is only exported to the server by the package?

Any help is appreciated! Thanks in advance

Upvotes: 1

Views: 1679

Answers (2)

kerosene
kerosene

Reputation: 891

Okay, thanks based on what @none posted, I did the following:

server_packageOne.js

doSomething = function(query) {
  //code here
}

then on the package.js of the package1

Package.onUse(function(api) {
//api.versionsFrom('[email protected]');

//api.use(['http'], ['server']); ['client', 'server']);

api.add_files('lib/server/server_packageOne.js', ['server']);
api.export('doSomething',['server']);
});

server_packageTwo.js

Meteor.methods({

methodOne: function() {
   var res = doSomething("1");
   //some stuff
   return res;
},
methodTwo: function() {
   return doSomething("2");
 }
});

on the package.js of the package2

Package.onUse(function(api) {
//api.versionsFrom('[email protected]');

//api.use(['http'], ['server']); ['client', 'server']);
api.use('name:package1');
api.add_files('lib/server/server_packageTwo.js', ['server']);
});

Upvotes: 1

none
none

Reputation: 1817

Try this function declaration in some file:

  doSomething = function(query) {
      //code here
  }

Now doSomething - global variable.

Upvotes: 4

Related Questions