【问题标题】:Getting a error inserting in to a Meteor Collection插入流星集合时出错
【发布时间】:2013-05-20 08:10:39
【问题描述】:

我开始与 Meteor 合作,我正在处理我的第一个问题。我正在尝试在我的收藏中插入一个项目。我收到以下控制台日志错误。有人可以帮助流星菜鸟吗?

插入失败:找不到方法

这是导致错误的行:

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

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

【问题讨论】:

标签: mongodb meteor


【解决方案1】:

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

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

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

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

【讨论】:

  • 一直都是这样吗?我的意思是 Collection.insert 尝试服务器和客户端插入,即使它是客户端集合?
  • @Mascarpone 如果集合仅在客户端。它不会调用服务器
【解决方案2】:

创建仅限本地的集合:

MyLocalCollection = new Collection(null);

(参考文档here

关于“_collection”:

_collection 是一个未记录的属性,在许多情况下行为异常。您可能不想使用它。

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

没有办法直接做到这一点。但是,创建现有集合的动态本地镜像非常容易(根据我的经验,这是任何复杂 UI 的方法)。见this post

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    相关资源
    最近更新 更多