Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

meteor / javascript function not working when in another file

This is interesting. I have a file structure as such:

/
/client/
/server/

The app I'm working on is working fine, I have many .js files in the /client/ folder, all separated into logical (to me) sections. They work fine when compiled.

I have added a new file though, called miscFunctions.js to the mix and added a simple function and saved:

function sessionDataReset(){
  //Set the New Organisation Session data to be false, meaning we're not adding a new organisation
  return Session.set('addingOrganisation', false);
};

This function, when called returns the following error:

Uncaught ReferenceError: sessionDataReset is not defined

When I move that exact code though to the .js file I'm calling it from it works fine.

Why is the error happening as I was of the understanding what I'm trying to do can be done with Meteor?

Any advice greatly appreciated.

Rob

like image 237
robster Avatar asked Feb 01 '26 12:02

robster


1 Answers

First try declaring your file this way:

sessionDataReset = function() {
  //Set the New Organisation Session data to be false, 
  //meaning we're not adding a new organisation
  return Session.set('addingOrganisation', false);
};

This ensures the function will be visible globally.
(@user1623481 Meteor wraps files as IIFE's when it compiles them, creating a function scope that was limiting the visibility of this function.)

This will most likely resolve it, but following this check the file load order in the Meteor Docs

like image 92
JeremyK Avatar answered Feb 04 '26 00:02

JeremyK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!