2015-04-28 50 views
0

嘗試使用具有數組的Mongodb集合。想要在模板中使用數組。流星如何從mongodb集合中使用數組

Index.html 

<body> 
{{>test}} 
</body> 
<template name="test"> 
{{ #each task}} 
<p>{{this}}</p> 
{{/each}} 
</template> 

The app.js 

Task= new Mongo.Collection("Tasks"); 

if (Meteor.isClient) { 
    // This code only runs on the client 
    Template.test.helpers({ 
    task: function() { 
     return Tasks.find({"cat":"TASK"}, {"_id":0, "ALL_TASKS":1}); 
    } 
    }); 
} 

if (Meteor.isServer){ 

    if (Task.find({}).count() === 0){ 
     Task.insert({"cat":"TASK", "ALL_TASKS":["t1","t2"]}) 
    } 

} 

它不起作用。我缺少什麼

回答

0

您應該再使用一個{{#each}}。您正在使用的{{#each}}正在返回集合對象而不是實際需要的數組。試試這個:

<template name="test"> 
    {{#each task}} 
     {{#each this.ALL_TASKS}} 
     <p>{{this}}</p> 
     {{/each 
    {{/each}} 
</template> 

請注意,this.ALL_TASKS現在循環訪問集合對象內部的數組。

你的助手應該是這樣的:

if (Meteor.isClient) { 
// This code only runs on the client 
    Template.test.helpers({ 
    task: function() { 
    return Tasks.find({"cat":"TASK"}); 
    } 
}); 
} 
+0

感謝的作品。 –