2017-02-09 38 views
0

我想跟着流星文档浏览: https://www.meteor.com/tutorials/blaze/collections 中加入了收集,并能够在控制台这样做是为了得到一个空数组回:为什么我继续获取未定义的任务?

Tasks.find().fetch() 

,而是我得到这个:

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

我不确定自从我遵循他们的文档以来我出错了,我相信我根据文档创建的imports文件夹的树结构是正确的,并且我迄今为止实现的代码是我的也正如他们的文档所建议的那样。

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

当您导入任务时它可能是缺少'.js'吗? – LPL

回答

0

Tasks定义的,但仅限于其导入的范围。由于它从未被宣布为全球性的,您将无法在浏览器的控制台中访问它。

如果你想看到的任务在控制台(您订阅的那些),只需更新您的辅助函数:

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

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

> meteor mongo 
> db.tasks.find() 
+0

查兹,我仍然在控制台和数据库中直接得到相同的错误,我什么都没有,甚至没有一个空的数组。 – Daniel

相关问题