【问题标题】:Why do I continue to get the Tasks is not defined?为什么我继续得到未定义的Tasks?
【发布时间】:2017-02-09 20:52:45
【问题描述】:

我正在尝试在此处遵循 Meteor 文档: https://www.meteor.com/tutorials/blaze/collections 在添加一个集合并能够通过在控制台中执行此操作来获取一个空数组:

Tasks.find().fetch()

但是我得到了这个:

Uncaught ReferenceError: Tasks is not defined
    at <anonymous>:1:1

由于我正在关注他们的文档,因此我不确定我哪里出错了,我相信我根据文档创建的导入文件夹的树结构是正确的,并且我到目前为止实现的代码也如建议的那样他们的文档。

这是客户端/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({

});

这是导入/api/tasks.js:

import {Mongo} from 'meteor/mongo';

export const Tasks = new Mongo.Collection('tasks');

这是服务器/main.js:

import { Meteor } from 'meteor/meteor';
import {Tasks} from '../imports/api/tasks';

Meteor.startup(() => {
  // code to run on server at startup
});

这是客户端/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>

【问题讨论】:

  • 导入任务时会不会是缺少.js

标签: javascript meteor meteor-blaze


【解决方案1】:

Tasks 定义,但仅在导入的范围内。由于它从未被声明为全局,因此您将无法在浏览器的控制台中访问它。

如果您想在控制台中查看任务(您正在订阅的任务),只需更新您的辅助函数:

Template.todos.helpers({
  tasks() {
    let tasks = Tasks.find({});
    console.log(tasks.fetch());
    return tasks;
  }
});

或者,您可以直接检查您的数据库:

> meteor mongo
> db.tasks.find()

【讨论】:

  • chaz,我仍然在控制台和数据库中直接得到同样的错误,我什么也没得到,甚至没有一个空数组。
猜你喜欢
  • 2019-10-31
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 2021-09-01
  • 2021-03-24
  • 2015-03-23
  • 2012-02-05
  • 1970-01-01
相关资源
最近更新 更多