Question

I'm running Meteor 0.8.0

I'm getting a ReferenceError from the browser console when attempting to insert to a Meteor Collection. FYI, I've removed the autopublish package for this app in order to play with publications and subscriptions.

Template

<head>
<title>itemsApp</title>
</head>

<body>
{{> name}}
{{> items}}
</body>

<template name="name">
<input type="text" />
</template>

<template name="items">
<h1>Items</h1>
<ul>
{{#each items}}
<li>{{name}} | {{category}}</li>
{{/each}}
</ul>
</template>

Code

var Items = new Meteor.Collection("items");

if (Meteor.isClient) {
Meteor.subscribe("items");

Template.items.items = function () {
return Items.find();
};
}

if (Meteor.isServer) {
Meteor.publish("items", function () {
return Items.find();
});
}

Now, from the browser console (FF28 & Chromium 33.0.1750.152 on Ubuntu 13.10), I'm getting

ReferenceError: Items is not defined

when I run:

Items.insert({name: "iPod", category : "Apple"});

Any ideas?

Thanks!

Was it helpful?

Solution

In Meteor, variables defined with var keywords are local to the file they're in. So in your case

var Items = new Meteor.Collection("items");

is local. Simply remove the keyword:

Items = new Meteor.Collection("items");

Now Items is a global variable and can be accessed in other files (and from console).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top