Meteor back-wards compatible, imports

Hello, I learned meteor through the University of London specializations courses. It was with an older version, not using imports. I know that there is a great value to the import statements but I wanted to try and see if the tutorial works without it.

I have the following html:

<body>
	<div class="container">
		<header>
			<h1>Todo List</h1>

			<form class="new-task">
				<input type="text" name="text" placeholder="Type to add new tasks" />
			</form>
		</header>

		<ul>
			{{#each tasks}} {{> task}} {{/each}}
		</ul>
	</div>


</body>

<template name="task">
	<li>{{text}}</li>
</template>

Then in the server folder (main.js)

var Tasks = new Mongo.Collection('tasks');

and finally in the client folder (main.js)

Template.body.helpers({
	tasks() {
		return Tasks.find({});
	},
});

Template.body.events({
	'submit .new-task' (event) {
		// Prevent default browser form submit
		event.preventDefault();

		// Get value from form element
		const target = event.target;
		const text = target.text.value;

		// Insert a task into the collection
		Tasks.insert({
			text,
			createdAt: new Date(), // current time
		});

		// Clear form
		target.text.value = '';
	},
});

However, when I run this. I get the error that Tasks is not defined.

Why am I getting this error, shouldn’t it communiciate with the server.js file and know that Tasks has been defined as a database? Even if I put in an import statement (which I am currently trying to avoid just to see if it can be done) it doesn’t work.

How can I fix this?

Put the Tasks collection definition in a common folder like /lib and don’t put var in front (or Tasks will just be defined for that particular file).

i.e. In

/lib/collections.js

or

/collections/tasks.js

write:

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

That will ensure Tasks is a global in all parts of the app.

Hey, thanks for the response.

I removed the var declaration and moved the statement into a collections.js file in the lib folder.

However, it then gave me the error that tasks is already defined in Mongo.

In order for it to work, I had to rename it in the collections file to pasks with a p.

Why did this happen, is tasks a protected word?

You have defined the collection already somewhere else in your app.

Search for “Collection(‘tasks’)” in your project folder’s files and you’ll find where (probably in the /server/main.js, since you had it there before following @babrahams advice )

I had removed it but perhaps it didn’t save.

I reset the server and now it works with ‘tasks’