Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a error inserting in to a Meteor Collection

Tags:

mongodb

meteor

I am starting to work with Meteor and I am running in to my first issue. I am trying to insert a item in to my collection. I get the below console log error. Can someone help a Meteor noob?

insert failed: Method not found

This is the line that cause the error:

Videos.insert({name: el.value});

My js file:

var Videos = new Meteor.Collection("videos");

if (Meteor.isClient) {
  Template.videoList.video = function() {
    return Videos.find();
  }

  Template.videoForm.events({
    'click button': function(e, t){
      var el = t.find("#name");
      Videos.insert({name: el.value});
      el.value = "";
    }
  });
}
like image 868
zero Avatar asked May 24 '13 16:05

zero


2 Answers

When you try Video.insert. Meteor is trying to insert both on the client and on the server as well. Meteor design it this way to help reflect the change instantly on the client(Latency Compensation).

When your Video collection is not defined on the server (not in Meteor.isServer wrap or file that can accessed by Server). It will throw the error you encountered.

If you want to insert to client only. You can access to it by _collection. So your insert statement would be Videos._collection.insert(values);

You can find more info here in this screen cast: http://www.eventedmind.com/feed/meteor-anatomy-of-a-collection-insert

like image 124
Bozhao Avatar answered Nov 21 '22 12:11

Bozhao


To create a local-only collection:

MyLocalCollection = new Collection(null);

(reference doc here)

About "_collection":

_collection is an undocumented property that behaves strangely in many cases. You might prefer not to use it.

About manipulating only the local cache of a client-server collection:

There is no way to do this directly. However, it is very easy to create a dynamic local mirror of an existing collection (in my experience, this is the way to go for any complex UI). See this post.

like image 43
steph643 Avatar answered Nov 21 '22 12:11

steph643