【问题标题】:Using Meteor with MongoDB在 MongoDB 中使用 Meteor
【发布时间】:2016-01-12 14:44:10
【问题描述】:

我无法让我非常简单的流星教程阅读我在 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>

【问题讨论】:

  • 您的助手定义了“任务”,但您的模板使用了“任务”。
  • 我也试过了,但没有成功@BrendanTurner
  • 您不能像那样将模板助手附加到正文。您必须制作一个模板(可能是“taskList”)并通过 {{> taskList}} 包含它,然后将您的模板助手指向 taskList 而不是正文。
  • 这令人惊讶,因为代码是从流星教程本身复制和粘贴的。你能模拟一些你所说的代码吗?我不确定你在说什么@BrendanTurner
  • 查看应用程序 url:meteor-hilldesigns.c9users.io 上面的内容,我没有看到错误。问题解决了吗?接受的答案有 cmets 表明它不起作用,但没有参考有效的实际解决方案

标签: javascript mongodb meteor cloud9-ide


【解决方案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

【讨论】:

  • 感谢您的帮助。我将它直接复制/粘贴到我的 cloud 9 工作区中,但它不起作用。也没有错误。你可以尝试创建一个,看看它是否适合你?或者我可以授予您访问我的权限?
  • 从我系统上的工作版本直接复制粘贴
  • 好吧,那么云9配置肯定有问题
  • 我用我的“流星列表”结果更新了我的结果,其中显示了软件包及其版本。对照你的检查/做流星更新
  • 试过并添加了自动发布,但它仍然没有成功。 :-(
【解决方案2】:

改变

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

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

只需将js文件中的“task”设为复数,这样它就会返回一个数组,每个语句都可以运行。

【讨论】:

  • 嗨!我试过了,但也没有用。还有其他想法吗?
  • 你能解释一下它是如何不起作用的吗?您是否在控制台中收到特定错误?
  • 没有错误。你可以在这里运行它ide.c9.io/hilldesigns/meteor 在终端@terrafirma9 中输入meteor --port $IP:$PORT
【解决方案3】:

你在 mongo 数据库中有什么吗?

尝试将任务添加到数据库中,看看您是否获得了页面更新。

使用:

meteor mongo

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

【讨论】:

  • 哪个数据库?流星还是本地?我在这两个文件中都有文件,但我仍然什么也没看到。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多