Strange issue, undefined vars


#1

Wtf?

Was wondering why i couldnt access any arrays or objects via the console, so I added this simple statement. Why is it coming out undefined?

Im loading a threejs webgl globe in a Template.onRendered, just for some context on the issue.


#2

You have a scope issue.


#3

Tyvm. Why would scope be an issue like that though when the only block its in is Meteor.isClient{} ?


#4

I’m actually unfamiliar with how scope works with regards to .isClient and .isServer I also don’t know what the best way to do what you are trying to do is without making everything global which doesn’t seem like an ideal solution. Maybe someone else has some insight to this. Sorry I couldn’t be more useful.


#5

For a variable to be available globally in Meteor, you should declare it without the var keyword. Meteor automatically wraps your files in functions to make sure local variables don’t leak out.


#6

Ty. Could you explain that 2nd part?


#7

He means that Meteor puts the code in a function, like this:

(function () {
    var hello = "hello";
})();

This automatically runs the function and makes hello a local variable inside the function. When you run code inside the console, the code would be ran outside this function. The code in the console would then have no access to the variable hello. Instead, if you omit the “var” before hello, it makes it a global variable; and the code in the console would then have access to the global variable.