简体   繁体   English

为什么没有定义流星模板对象?

[英]Why is Meteor Template Object Not Defined?

I've been following a tutorial to make a simple forum, after finally getting all the code get together, it tells me 'Template is not defined' 我一直在按照教程制作一个简单的论坛,在最终得到所有代码后,它告诉我'模板未定义'

Code of forum.html forum.html代码

<head>
  <title>Forum</title>
</head>
<body>
  {{> form}}
  {{> posts}}
</body>

<template name="posts">
  <h1>Posts</h1>
  <ul>
    {{#each posts}}
      <li>
        <h3>{{title}}</h3>
        <p>{{body}}</p>
      </li>
    {{/each}}
  </ul>
</template>


<template name="form">
  <form>
    <label>Post Title:
      <input type="text" id="title" />
    </label>
    <label>Post Body:
      <textarea id="body"></textarea>
    </label>
    <input type="submit" value="Submit" id="submit"/>
  </form>
</template>

Code of forum.js: forum.js代码:

var Posts = new Meteor.Collection('posts');
  if (Meteor.isClient) {
    Template.posts.helpers({
      Posts: function() {
        return Posts.find();
      }
    });
  }

Template.form.events = {
  'click #submit': function(event){
    event.preventDefault();
    var title = $('#title').val();
    var body = $('#body').val();
    Posts.insert({
      title: title,
      body: body
    });
    $('#title, #body').val('');
  }
};

Here's some of the output I get from meteor 这是我从流星得到的一些输出

W20150211-02:01:42.086(0)? (STDERR)           
W20150211-02:01:42.088(0)? (STDERR) /home/ubuntu/.meteor/packages/meteor-tool/.1.0.40.1ef5dzv++os.linux.x86_64+web.browser+web.cordova/meteor-tool-os.linux.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:173
W20150211-02:01:42.088(0)? (STDERR)                                             throw(ex);
W20150211-02:01:42.088(0)? (STDERR)                                                   ^
W20150211-02:01:42.091(0)? (STDERR) ReferenceError: Template is not defined
W20150211-02:01:42.091(0)? (STDERR)     at app/forum.js:10:1
W20150211-02:01:42.091(0)? (STDERR)     at app/forum.js:23:3
W20150211-02:01:42.091(0)? (STDERR)     at /home/ubuntu/workspace/forum/.meteor/local/build/programs/server/boot.js:205:10
W20150211-02:01:42.092(0)? (STDERR)     at Array.forEach (native)
W20150211-02:01:42.092(0)? (STDERR)     at Function._.each._.forEach (/home/ubuntu/.meteor/packages/meteor-tool/.1.0.40.1ef5dzv++os.linux.x86_64+web.browser+web.cordova/meteor-tool-os.linux.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
W20150211-02:01:42.092(0)? (STDERR)     at /home/ubuntu/workspace/forum/.meteor/local/build/programs/server/boot.js:116:5
=> Exited with code: 8
=> Your application is crashing. Waiting for file change.

There's 2 problems with your code : 你的代码有两个问题:

  • Template definition is not available on server so you need to wrap the Template.form definition in a Meteor.isClient condition, or better yet, separate your code using client and server directories. 模板定义在服务器上不可用,因此您需要将Template.form定义包装在Meteor.isClient条件中,或者更好的是,使用clientserver目录分隔您的代码。

  • Correct event maps definition needs to use this syntax : Template.form.events({...}); 正确的事件映射定义需要使用以下语法: Template.form.events({...}); not Template.form.events={...}; 不是Template.form.events={...};

The reason why you are seeing the error that you are is because your second reference to the Template object is not specified to be running explicitly on the client, like your first reference to Template is. 您看到错误的原因是因为您没有指定对Template对象的第二次引用在客户端上显式运行,就像您对Template的第一次引用一样。 The Template object is only available on the client, as detailed in this section of the Meteor documentation. Template对象仅在客户端上可用,详见Meteor文档的此部分 You would just simply need to bring the closing bracket of your if(Meteor.isClient){} code block down to be below your Template.form.events definition. 您只需要将if(Meteor.isClient){}代码块的if(Meteor.isClient){}括号降低到Template.form.events定义之下。

This, however, brings up the subject of application structure and how to avoid issues like this in the future during further development of your application. 然而,这提出了应用程序结构的主题以及如何在应用程序的进一步开发过程中避免将来出现类似问题。 If you take a look at this documentation , it is highly recommended that you divide your JS code up into at least two different locations to avoid encountering issues like this in the future. 如果您查看本文档 ,强烈建议您将JS代码分成至少两个不同的位置,以避免将来遇到类似这样的问题。 I would suggest moving your var Posts = new Meteor.Collection('posts'); 我建议移动你的var Posts = new Meteor.Collection('posts'); line into a JS file within a top-level server folder ( name_of_app_directory/server ), and moving all of the other JS code into a JS file within a top-level client folder ( name_of_app_directory/client ). 在顶级server文件夹( name_of_app_directory/server )中输入JS文件,并将所有其他JS代码移动到顶级client文件夹( name_of_app_directory/client )中的JS文件中。 This way, you can eliminate the need to also include the if(Meteor.isClient){} block in your code, and no more chance for seeing the error that you have. 这样,您就无需在代码中包含if(Meteor.isClient){}块,也无需再查看错误。

Also, final thing to consider. 另外,最后要考虑的事情。 When defining your template events object, define it similarly to how you have already defined your template helpers object ( Template.form.events({...}) ). 定义模板events对象时,请按照已定义模板helpers对象的方式( Template.form.events({...}) )进行定义。 For more information on this, see this documentation . 有关此内容的更多信息,请参阅此文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM