我正在尝试按照Meteor文档: https://www.meteor.com/tutorials/blaze/collections 添加一个集合并通过在控制台中执行此操作来获取空数组:
Tasks.find().fetch()
但我得到了这个:
Uncaught ReferenceError: Tasks is not defined
at <anonymous>:1:1
我不确定我在哪里出错了,因为我正在关注他们的文档,我相信我根据文档创建的imports文件夹的树结构是正确的,我到目前为止实现的代码也是从他们的文档。
这是client / main.js:
import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
import {Tasks} from '../imports/api/tasks';
import './main.html';
Template.hello.onCreated(function helloOnCreated() {
// counter starts at 0
this.counter = new ReactiveVar(0);
});
// templates can have helpers which are just functions and events and this
// particular event is a click event
Template.todos.helpers({
tasks() {
return Tasks.find({});
},
});
Template.todos.events({
});
这是imports / api / tasks.js:
import {Mongo} from 'meteor/mongo';
export const Tasks = new Mongo.Collection('tasks');
这是server / main.js:
import { Meteor } from 'meteor/meteor';
import {Tasks} from '../imports/api/tasks';
Meteor.startup(() => {
// code to run on server at startup
});
这是client / main.html:
<head>
<title>tasklist</title>
</head>
<body>
<h1>Welcome to Meteor!</h1>
{{> todos}}
{{> info}}
</body>
<template name="todos">
</template>
<template name="info">
<h2>Learn Meteor!</h2>
<ul>
<li><a href="https://www.meteor.com/try" target="_blank">Do the Tutorial</a></li>
<li><a href="http://guide.meteor.com" target="_blank">Follow the Guide</a></li>
<li><a href="https://docs.meteor.com" target="_blank">Read the Docs</a></li>
<li><a href="https://forums.meteor.com" target="_blank">Discussions</a></li>
</ul>
</template>
答案 0 :(得分:0)
Tasks
是定义的,但仅限于导入的范围。由于它从未被声明为全局,因此您将无法在浏览器的控制台中访问它。
如果您想在控制台中查看任务(您订阅的任务),只需更新您的帮助程序功能:
Template.todos.helpers({
tasks() {
let tasks = Tasks.find({});
console.log(tasks.fetch());
return tasks;
}
});
或者,您可以直接检查数据库:
> meteor mongo
> db.tasks.find()