3

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

4
  • Is sessionDataReset being called before miscFunctions.js is loaded? Commented Nov 20, 2015 at 0:40
  • That's a great question. Meteor compiles them together into one large file. I hadn't considered this. How are we meant to know which order the files are being loaded in when they're separated? Commented Nov 20, 2015 at 0:50
  • it is declared function and it should hoisted to top anyways. is it wrapped by another code? Commented Nov 20, 2015 at 2:00
  • It's being called from a function that's in the isClient area (for meteor). The actual file itself simply sits in the "client" folder meaning it will be joined with all the other files in there before being used. I wonder if it's perhaps being joined to the end of the file? There must be a way to have it be added to the top of the file when joined? Commented Nov 20, 2015 at 2:02

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

This worked perfectly. I am new to this so thank you for the help here. I now understand the difference between a global and local function. Thank you also for the file load order / file structure link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.