Uncaught ReferenceError: template is not defined

1,044 views
Skip to first unread message

ServerMeta

unread,
Jan 15, 2015, 11:58:45 AM1/15/15
to meteo...@googlegroups.com
I've met this error, and probably I'm doing something very very basic wrong.

I'm trying to update DOM elements, here's what I tried:

meteor create test

I changed the following files:

test.html

<template name="hello">
  <button id="bot">Click Me</button>
  <p>You've pressed the button {{counter}} times.</p>
</template>

test.js



  Template.hello.events({
    'click button': function () {
      // increment the counter when button is clicked
      Session.set("counter", Session.get("counter") + 1);
      var nameVal = template.find('#bot');
      console.log(nameVal);
    }
  });
}


The console gives me the error "Uncaught ReferenceError: template is not defined".

What am I doing wrong? I've already tried reinstalling meteor.

Jan Hendrik Mangold

unread,
Jan 15, 2015, 1:02:15 PM1/15/15
to meteo...@googlegroups.com

On Jan 15, 2015, at 08:58, ServerMeta <alberto...@gmail.com> wrote:

  Template.hello.events({
    'click button': function () {
      // increment the counter when button is clicked
      Session.set("counter", Session.get("counter") + 1);
      var nameVal = template.find('#bot');
      console.log(nameVal);
    }
  });
}


The console gives me the error "Uncaught ReferenceError: template is not defined".

What am I doing wrong? I’ve already tried reinstalling meteor.

try


  Template.hello.events({
    ‘click button’: function (event, template) {

Kelly Copley

unread,
Jan 15, 2015, 9:02:37 PM1/15/15
to meteo...@googlegroups.com

Looks to me as if you might have this running in shared space..  Template is only defined on the client so wrapping in a Meteor.isServer or moving the code to a server directory should fix this error.

--
You received this message because you are subscribed to the Google Groups "meteor-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to meteor-talk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/meteor-talk/183499AD-607A-4AFC-8211-460121619384%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Avital Oliver

unread,
Jan 15, 2015, 9:13:17 PM1/15/15
to meteo...@googlegroups.com
It's what Jan said. `template` (which is not `Template`) is the second argument to the event handler, not a magic global. http://docs.meteor.com/#/full/eventmaps

Chet Murphy

unread,
Jan 16, 2015, 12:05:26 AM1/16/15
to meteo...@googlegroups.com
"template" is not defined. It should be set to Template.Instance(). Just add

var template = Template.instance();

before your template.find() call.
Reply all
Reply to author
Forward
0 new messages