插入Meteor集合时出错

zer*_*ero 7 mongodb meteor

我开始和Meteor合作,我正在讨论第一期.我想在我的收藏中插入一个项目.我得到以下控制台日志错误.有人可以帮助流星菜鸟吗?

插入失败:找不到方法

这是导致错误的行:

Videos.insert({name: el.value});
Run Code Online (Sandbox Code Playgroud)

我的js文件:

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 = "";
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

Boz*_*hao 12

当您尝试Video.insert.Meteor试图在客户端和服务器上同时插入它们.Meteor以这种方式设计它以帮助客户即时反映变化(延迟补偿).

未在服务器上定义视频集合时(不在Meteor.isServer包中或可由服务器访问的文件中).它会抛出你遇到的错误.

如果您只想插入客户端.您可以通过_collection访问它.所以你的insert语句是Videos._collection.insert(values);

您可以在此屏幕上找到更多信息:http://www.eventedmind.com/feed/meteor-anatomy-of-a-collection-insert


ste*_*643 6

要创建仅限本地的集合:

MyLocalCollection = new Collection(null);

(参考文档在这里)

关于"_collection":

_collection是一种未记录的属性,在许多情况下表现得很奇怪.您可能不想使用它.

关于仅操作客户端 - 服务器集合的本地缓存:

没有办法直接这样做.但是,创建现有集合的动态本地镜像非常容易(根据我的经验,这是任何复杂UI的方法).看这篇文章.