2016-01-12 85 views
0

我很難讓我非常簡單的流星教程在Mongodb中讀取我的集合並打印到頁面上。這是流星網站上的官方tutuorial。任何幫助將非常感激。如果有人想連接到工作區並進行更改,請告訴我,我可以授予訪問權限。在MongoDB中使用流星

這裏是我的工作空間的鏈接:https://ide.c9.io/hilldesigns/meteor

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

if (Meteor.isClient) { 
// This code only runs on the client 
Template.body.helpers({ 
    tasks: function() { 
    return Tasks.find({}); 
    } 
}); 
} 

下面是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> 
+0

你的助手定義了「任務」,但你的模板使用「任務」。 –

+0

我試過了,沒有成功@BrendanTurner – MontyTheMack

+0

你不能附加一個模板助手這樣的身體。你必須製作一個模板(可能是'taskList')並通過{{> taskList}}包含它,然後將你的模板助手指向taskList而不是body。 –

回答

1

不能確定某個助手安裝到車體,它不是在1.2.1(最新版本)的支持。如果您在瀏覽器中打開控制檯,它應該顯示關於無法訪問未定義助手的錯誤。

所以,使其工作...

<head> 
<title>Todo List</title> 
</head> 

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

<template name="todos"> 
    <ul> 
    {{#each tasks}} 
    {{> task}} 
    {{/each}} 
</ul> 
</template> 

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

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

if (Meteor.isClient) { 
// This code only runs on the client 
Template.todos.helpers({ 
    tasks: function() { 
    return Tasks.find({}); 
    } 
}); 
} 

工作正常

這裏是我的流星名單

autopublish   1.0.4 (For prototyping only) Publish the entire database to all clients 
blaze-html-templates 1.0.1 Compile HTML templates into reactive UI with Meteor Blaze 
ecmascript   0.1.6* Compiler plugin that supports ES2015+ in all .js files 
es5-shim    4.1.14 Shims and polyfills to improve ECMAScript 5 support 
insecure    1.0.4 (For prototyping only) Allow all database writes from the client 
jquery    1.11.4 Manipulate the DOM using CSS selectors 
meteor-base   1.0.1 Packages that every Meteor app needs 
mobile-experience  1.0.1 Packages for a great mobile user experience 
mongo     1.1.3 Adaptor for using MongoDB and Minimongo over DDP 
session    1.1.1 Session variable 
standard-minifiers 1.0.2 Standard minifiers used with Meteor apps by default. 
tracker    1.0.9 Dependency tracker to allow reactive callbacks 

和流星是1.2。 1

+0

感謝您的協助。我將其直接複製/粘貼到我的雲9工作區,並且不起作用。也沒有錯誤。你可以嘗試創建一個,看看它是否適合你?或者我可以授予您對我的訪問權限? – MontyTheMack

+0

這是從我的系統上的工作版本直接複製粘貼 –

+0

好吧,一定是錯誤的雲9配置然後 – MontyTheMack

1

變化

Template.body.helpers({ 
    task: function() { 
    return Tasks.find({}); 
    } 
}); 

Template.body.helpers({ 
    tasks: function() { 
    return Tasks.find({}); 
    } 
}); 

只需以js文件複數形式創建「任務」,就可以返回每個語句可以運行的數組。

+0

嗨!我嘗試過,但也沒有工作。任何其他想法? – MontyTheMack

+0

你能解釋它是如何不起作用的嗎?您是否在控制檯中收到特定錯誤? – terrafirma9

+0

沒有錯誤。你可以在這裏運行https://ide.c9.io/hilldesigns/meteor,在終端輸入'meteor --port $ IP:$ PORT' @ terrafirma9 – MontyTheMack

0

你有什麼在mongo數據庫?

嘗試添加一個任務到數據庫,看看你是否得到頁面更新。

使用:

meteor mongo 

db.tasks.insert({ text: "Hello world!", createdAt: new Date() }); 
+0

哪個數據庫?流星還是本地?我有兩份文件,但我仍然沒有看到任何內容。 – MontyTheMack