我试图在我的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();
}
});
我在这里应该发生的事情的概念是&#34; placesLived&#34;模板被添加到页面中,该模板调用&#34; places&#34; js文件中的函数 - 返回所有TimeAndSpace文档 - 最后是&#34; placesLived&#34;模板循环返回那些返回的文档,将每个字段放在&#34; td&#34;
中但是,保存这些更改(重新启动Meteor应用程序)会对Meteor字段造成严重破坏:命令提示符会对我施加以下诽谤(不要与小行星混淆):
Meteor机器显然没有被逗乐。我的代码/需要改变的内容是什么?
答案 0 :(得分:2)
您的助手正在客户端上运行&amp;服务器。在它周围放一个.sdf
&amp;你没事。
任何时候你在控制台上都会看到漂亮的紫色/蓝色,这意味着你的服务器出了问题。当它显示isClient
时,它告诉您服务器找不到名为Template is not defined
的内容。