2015-02-11 55 views
7

我一直在下面的教程做一個簡單的論壇後,終於讓所有的代碼湊在一起,它告訴我「模板沒有定義」爲什麼流星模板對象沒有定義?

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> 

碼的碼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(''); 
    } 
}; 

下面是一些輸出,我從流星得到

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. 
+0

fyi - 我有這個相同的錯誤,因爲我不小心將我的clent模板文件夾之外的客戶端文件夾移動到根。所以我從/partners/partner.html移回到/client/partners/partner.html都更好。 – xeo 2016-01-10 22:14:24

回答

7

有兩個問題與您的代碼:所以你需要用的Template.form定義一個Meteor.isClient條件,或者更好的是,使用clientserver目錄劃分代碼

  • 模板定義不可用在服務器上。

  • 正確的事件映射定義需要使用這個語法:Template.form.events({...});Template.form.events={...};

1

爲什麼你看到,你是因爲沒有指定你的第二個參考Template對象是錯誤的原因明確地在客戶端上運行,就像你第一次參考Template一樣。 Template對象僅在客戶端可用,詳見Meteor文檔的this section。您只需簡單地將if(Meteor.isClient){}代碼塊的右括號降至Template.form.events定義以下。

然而,這提出了應用程序結構的主題,以及在未來的應用程序開發過程中如何避免這樣的問題。如果你看看this documentation,強烈建議你將JS代碼分成至少兩個不同的位置,以避免將來遇到類似的問題。我建議將var Posts = new Meteor.Collection('posts');行轉換爲頂級server文件夾(name_of_app_directory/server)內的JS文件,並將其他所有JS代碼移動到頂級文件夾(name_of_app_directory/client)內的JS文件中。這樣,您就不需要在代碼中包含if(Meteor.isClient){}塊,也不會再有機會看到您擁有的錯誤。

另外,最後要考慮的事情。在定義您的模板events對象時,請按照如何定義模板helpers對象(Template.form.events({...}))的方式對其進行定義。有關更多信息,請參見this documentation