pipedreambomb
pipedreambomb

Reputation: 4030

Meteor global 'this' seems to be undefined

I'm trying to factor out my increasingly big single CoffeeScript file in my Meteor project, and have followed the official advice on scoping global variables using this. However, even something simple like:

console.log("this=" + this)
@gave =
  Transactions: new Meteor.Collection("Transactions")
  Causes: new Meteor.Collection("Causes")

Generates terminal errors and the server won't start:

=> Meteor server restarted
this=undefined

/home/g/workspace/gave/.meteor/local/build/server/server.js:321
}).run();
 ^
TypeError: Cannot set property 'gave' of undefined
    at app/gave.coffee.js:6:11

According to the advice linked above,

Global variables can be set in CoffeeScript by using this (or CoffeeScript's @ shorthand), because at the top level this refers to the global namespace (window on the client and global on the server).

So, I can't really figure out where I'm going wrong. Can you? :)

Upvotes: 0

Views: 380

Answers (1)

Ven
Ven

Reputation: 19040

See ES5 - 15.3.4.4.

NOTE The thisArg value is passed without modification as the this value. This is a change from Edition 3, where a undefined or null thisArg is replaced with the global object and ToObject is applied to all other values and that result is passed as the this value.

So, with the "use strict", meteor's .call(null) will effectively give you a this == null =).

Upvotes: 2

Related Questions