0

I have defined some functions in server/methods.js which I use in some of my methods such as:

function randomIntFromInterval(min, max) {
  return Math.floor(Math.random() * (max - min + 1) + min);
}

If I want to use the functions in my methods, I have to place them in server/methods.js. Why can't I place the functions in lib/utils.js? I thought that files in lib/ would be called first, so functions there would be accessible in all other files.

Jamgreen
  • 10,329
  • 29
  • 113
  • 224
  • possible duplicate of [How do I change the order in which Meteor loads Javascript files?](http://stackoverflow.com/questions/10693113/how-do-i-change-the-order-in-which-meteor-loads-javascript-files) – franciscod Jul 15 '15 at 10:07
  • typo between `lib` and `libs`? – franciscod Jul 15 '15 at 10:08
  • Sorry, it is `lib`. Just a typo in this question – Jamgreen Jul 15 '15 at 10:09
  • The answer on http://stackoverflow.com/questions/10693113/how-do-i-change-the-order-in-which-meteor-loads-javascript-files says that files in `lib` will be loaded first, but it's not the case here. Maybe I can't use 'publicly defined' UDFs inside server methods? and is it correct to only define methods on the server? – Jamgreen Jul 15 '15 at 10:10
  • what happens when you place your function inside `lib/utils.js`? – franciscod Jul 15 '15 at 10:15

1 Answers1

3

By defining your function like this function randomIntFromInterval(min, max) {...} its availability will actually be limited to the lib/utils.js file and your function won't be available from any other JS files on the server.

You have to declare your function like this in order to put it in the global scope and make it accessible from other JS files:

randomIntFromInterval = function (min, max) {
  ...
};

Note the absence of the var keyword which would also limit the function availability to the lib/utils.js file.

255kb - Mockoon
  • 6,657
  • 2
  • 22
  • 29