【发布时间】:2019-09-04 15:43:54
【问题描述】:
我正在通过 Eduonix.com 参加全栈课程。似乎不推荐使用某些代码语法,因为我必须安装多个旧版本的东西才能通过某些部分。当我来到 Meteor.js 部分时,这并没有帮助,所以我安装了最新的并进行了一些搜索,使我能够完成第一课。我在第二节课上尝试了同样的方法(当我遇到这个错误时),但我发现的任何东西都没有运气。
我在尝试使用时收到此错误
todos.find().fetch()
在浏览器的控制台中。
相关文件结构:
client
--main.html
--main.js
lib
--collections.js
在课程课中,行
import { Todos } from '../lib/collections';
main.js 和行中不存在
export const Todos = new Mongo.Collection('todos');
在 collections.js 中显示为
Todos = new Mongo.Collection('todos');
我尝试过改变
import { Todos } from '../lib/collections';
到
import { Todos } from '/lib/collections';
但它什么也没做。
我也试过添加
Todos = new Mongo.Collection('todos');
到 main.js,但我得到一个错误,说“todos”已经定义(只是在我尝试运行控制台命令时得到相同的未定义错误,因为不知何故它都是已定义但仍未定义)。
在发这篇文章之前,我根据在网上查找类似问题进行了这些更改,希望它会像添加一样节省我的时间
import './main.html';
to main.js 是在我收到与此错误之前的类似未定义错误相关的错误时执行的。
main.html
<head>
<title>QuickTodos</title>
</head>
<body>
{{> main}}
</body>
<Template name="main">
<header>
<h1>{{title}}</h1>
</header>
<ul>
{{#each todos}}
{{> todo}}
{{/each}}
</ul>
</template>
<template name="todo">
<li>{{text}}</li>
</Template>
main.js
import { Template } from 'meteor/templating';
import './main.html';
import { Todos } from '../lib/collections';
const todos = [
{text:'Pickup kids from school'},
{text:'Go food shopping'},
{text:'Meeting with boss'}
];
Template.main.helpers({
title(){
return 'QuickTodos';
},
todos(){
return todos;
}
});
collections.js
import {Mongo} from 'meteor/mongo';
export const Todos = new Mongo.Collection('todos');
当我跑步时
todos.find().fetch()
我希望得到一个空数组,但我得到了:
VM1905:1
Uncaught ReferenceError: todos is not defined
at <anonymous>:1:1
我做错了什么?
【问题讨论】: