Can't insert into a client-side collection


#1

I’ve copied the below code from the Meteor Guide into my /client/main.js file:

// Only on the client
Todos = new Mongo.Collection('Todos');

// This line is changing an in-memory Minimongo data structure
Todos.insert({_id: 'my-todo'});
// And this line is querying it
const todo = Todos.findOne({_id: 'my-todo'});
// So this happens right away!
console.log(todo);

But I’m getting the following message in my console:

insert failed: Method '/Todos/insert' not found

I still have the insecure package installed.

Can anyone shed any light on this?


#2

To get rid of the error I think you need to also define the collection on the server:

// in /server/main.js
Todos = new Mongo.Collection('Todos');

If you want to use a Local collection you need to pass null to Mong.Collection:

// local collection
Todos = new Mongo.Collection(null);

#3

Yeah, I think this article is misleading - we should make it clear that you need to pass the null to be able to insert without it trying to call the server.


#4

Ok thanks for the replies / explanations. I’m writing a package that interfaces OpenUI5 with Meteor and it relies on named Mongo collections to access collections via dburles:collection-helpers. I was hoping to be able to have a named, client-only collection to manage UI state.

Not to worry, there are other ways to skin the cat.

Cheers