最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot read property 'helpers' of undefined - Stack Overflow

programmeradmin5浏览0评论

Meteor came out of beta, and I am very excited to use it. I installed it on my windows machine (from /) and created an app. Here's the code:

html:

<!-- simple-todos.html -->
<head>
  <title>Todo List</title>
</head>

<body>
  <div class="container">
    <header>
      <h1>Todo List</h1>
    </header>

    <ul>
      {{#each tasks}}
        {{> task}}
      {{/each}}
    </ul>
  </div>
</body>

<template name="task">
  <li>{{text}}</li>
</template>

javascript:

// simple-todos.js
if (Meteor.isClient) {
  // This code only runs on the client
  Template.body.helpers({
    tasks: [
      { text: "This is task 1" },
      { text: "This is task 2" },
      { text: "This is task 3" }
    ]
  });
}

It is the exact same code as in the official meteor tutorial. If I run the example, the title is rendered just fine. The list on the other hand doesn't appear at all. Somehow, the helpers do not work. I get the following javascript error:

Uncaught TypeError: Cannot read property 'helpers' of undefined

In the meteor console, no error or warning was printed. I am very excited about meteor and I would love to use it in the future for production apps, so please help me with this (probably very simple) problem. I hope it is not only a problem in windows (meteor hasn't yet been officially released for windows).

Meteor came out of beta, and I am very excited to use it. I installed it on my windows machine (from http://win.meteor./) and created an app. Here's the code:

html:

<!-- simple-todos.html -->
<head>
  <title>Todo List</title>
</head>

<body>
  <div class="container">
    <header>
      <h1>Todo List</h1>
    </header>

    <ul>
      {{#each tasks}}
        {{> task}}
      {{/each}}
    </ul>
  </div>
</body>

<template name="task">
  <li>{{text}}</li>
</template>

javascript:

// simple-todos.js
if (Meteor.isClient) {
  // This code only runs on the client
  Template.body.helpers({
    tasks: [
      { text: "This is task 1" },
      { text: "This is task 2" },
      { text: "This is task 3" }
    ]
  });
}

It is the exact same code as in the official meteor tutorial. If I run the example, the title is rendered just fine. The list on the other hand doesn't appear at all. Somehow, the helpers do not work. I get the following javascript error:

Uncaught TypeError: Cannot read property 'helpers' of undefined

In the meteor console, no error or warning was printed. I am very excited about meteor and I would love to use it in the future for production apps, so please help me with this (probably very simple) problem. I hope it is not only a problem in windows (meteor hasn't yet been officially released for windows).

Share Improve this question asked Nov 8, 2014 at 16:08 LucaLuca 5581 gold badge4 silver badges18 bronze badges 1
  • First thing I'd do is add console.dir(Template); before the attempted call to .helpers(). The issue is that .body is undefined, so it'd be useful to figure out what Template actually looks like. – Pointy Commented Nov 8, 2014 at 16:11
Add a ment  | 

1 Answer 1

Reset to default 5

Template needs to be defined by <template name="xxx">.

There is no helper on the <body> element.

You could use a global helper for this, though:

Template.registerHelper("tasks", function() {
    return [....]
});

An alternative is to use <template name="body">:

<body>
    {{>body}}
</body>

<template name="body">
  <div class="container">
    <header>
      <h1>Todo List</h1>
    </header>

    <ul>
      {{#each tasks}}
        {{> task}}
      {{/each}}
    </ul>
  </div>
</template>
发布评论

评论列表(0)

  1. 暂无评论