为什么我尝试在我的 Meteor 应用程序中获取和显示 MongoDB 文档时崩溃了?

Why is my attempt to fetch and show MongoDB documents in my Meteor app crashing?

我正在尝试在我的 Meteor 应用程序中获取和显示一些 MongoDB 文档。我正在尝试使用 the docs here 作为基础。

我添加的HTM是这样的:

{{> placesLived}}
    . . .
<template name="placesLived">
  <table style="width:60%">
  {{#each places}}
  <tr>
    <td>{{ts_city}}</td>
    <td>{{ts_state}}</td>
    <td>{{ts_yearin}}</td>
    <td>{{ts_yearout}}</td>
  </tr>
  {{/each}}
</table>
</template>

...因此整个 .html 文件现在是这样的:

<head>
  <title>timeandspace</title>
</head>

<body>
  <h1>A List of the Places I Have Lived</h1>
  {{> addTimeSpaceForm}}
  {{> placesLived}}
</body>

<template name="addTimeSpaceForm">
<form>
    <label for="city">City</label>
    <input type="text" name="city" id="city">
    <br/>
    <label for="state">State</label>
    <input type="text" name="state" id="state">
    <br/>
    <label for="yearin">Year Arrived</label>
    <input type="text" name="yearin" id="yearin">
    <br/>
    <label for="yearout">Year Departed</label>
    <input type="text" name="yearout" id="yearout">
    <br/>
    <input type="submit" name="insertdocument" id="insertdocument" value="Add Place Lived">
</form>
</template>

<template name="placesLived">
  <table style="width:60%">
  {{#each places}}
  <tr>
    <td>{{ts_city}}</td>
    <td>{{ts_state}}</td>
    <td>{{ts_yearin}}</td>
    <td>{{ts_yearout}}</td>
  </tr>
  {{/each}}
</table>
</template>

我加的Javascript是这样的:

Template.placesLived.helpers({
  places: function () {
    // this helper returns a cursor of all of the documents in the collection
    return TimeAndSpace.find();
  }
});

...所以整个 .js 文件现在是这样的:

TimeAndSpace = new Mongo.Collection('timeAndSpace');

if (Meteor.isClient) {
    Template.addTimeSpaceForm.events({
        'submit form': function(event){
            event.preventDefault();
            var city = event.target.city.value;
            var state = event.target.state.value;
            var yearin = event.target.yearin.value;
            var yearout = event.target.yearout.value;

            Meteor.call('insertLocationData', city, state, yearin, yearout);
        }
    });
}

if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup
  });

  Meteor.methods({
      'insertLocationData': function(city, state, yearin, yearout){
          console.log('attempting to insert a record');
          TimeAndSpace.insert({
              ts_city: city,
              ts_state: state,
              ts_yearin: yearin,
              ts_yearout: yearout
          });
      }
  });
}

Template.placesLived.helpers({
  places: function () {
    // this helper returns a cursor of all of the documents in the collection
    return TimeAndSpace.find();
  }
});

我对这里应该发生的事情的看法是 "placesLived" 模板被添加到页面,该模板调用 js 文件中的 "places" 函数 -- returns 所有 TimeAndSpace 文档——最后 "placesLived" 模板循环遍历那些返回的文档,将每个字段放在 "td"

但是,保存这些更改(这会重新启动 Meteor 应用程序)会对 Meteor 场造成严重破坏:命令提示符对我进行了以下诽谤(不要与小行星混淆):

Meteor机子显然不被逗乐了。我的代码有什么问题/需要更改什么?

您的助手在客户端和服务器上 运行。在它周围放一个 isClient 就可以了。

只要您在控制台上看到 purple/blue 漂亮的颜色,就意味着您的服务器出现了问题。当它显示 Template is not defined 时,告诉您服务器找不到名为 Template.

的内容