Getting a error inserting in to a Meteor Collection

岁酱吖の 提交于 2020-01-21 04:41:29

问题


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 = "";
    }
  });
}

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/16739810/getting-a-error-inserting-in-to-a-meteor-collection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!