【问题标题】:How should I insert into Meteor collection using autoform/collection2?我应该如何使用 autoform/collection2 插入 Meteor 集合?
【发布时间】:2014-08-12 01:02:56
【问题描述】:

我正在尝试使用 Meteor 制作自动生成书籍示例。我应该怎么做 Books.insert ?

我看到了例子:

Books.insert({title: "Ulysses", author: "James Joyce"}, function(error, result) {
  //The insert will fail, error will be set,
  //and result will be undefined or false because "copies" is required.
  //
  //The list of errors is available on
  //`error.invalidKeys` or by calling
  Books.simpleSchema().namedContext().invalidKeys()
});

我不完全确定我应该如何将它与我的其余代码联系起来:

if (Meteor.isClient) {

   Books = new Meteor.Collection("books");

   var Schemas = {};

  Schemas.Book = new SimpleSchema({

  title: {
   type: String,
   label: "Title",
   max: 200,
   optional: true
  },
  author: {
   type: String,
   label: "Author",
   optional: true
  },
  copies: {
   type: Number,
   label: "Number of copies",
   min: 0,
   optional: true
  },
  lastCheckedOut: {
    type: Date,
    label: "Last date this book was checked out",
    optional: true
  },
  summary: {
    type: String,
    label: "Brief summary",
    optional: true,
    max: 1000
  }
});

Books.attachSchema(Schemas.Book);

}

有人可以给我任何建议吗?

我在想我需要类似这样的东西:

Template.bookform.events({
'click btn.submit': function () {
  var form = document.getElementById("formID").value;
  Books.insert(form);
}
});

提前致谢! :)

【问题讨论】:

    标签: meteor meteor-collection2


    【解决方案1】:

    我从未使用过 autoform,但在 documentation 中它说它已经为您提供了“自动插入和更新事件,以及自动反应式验证”。

    因此不需要指定您自己的事件处理程序。

    docs 中,您还可以找到书籍示例。我只是从那里复制:

    JS

    Books = new Meteor.Collection("books", {
        schema: {
            title: {
                type: String,
                label: "Title",
                max: 200
            },
            author: {
                type: String,
                label: "Author"
            },
            copies: {
                type: Number,
                label: "Number of copies",
                min: 0
            },
            lastCheckedOut: {
                type: Date,
                label: "Last date this book was checked out",
                optional: true
            },
            summary: {
                type: String,
                label: "Brief summary",
                optional: true,
                max: 1000
            }
        }
    });
    
    if (Meteor.isClient) {
      Meteor.subscribe("books");
    }
    
    if (Meteor.isServer) {
      Meteor.publish("books", function () {
        return Books.find();
      });
    }
    

    HTML

    <head>
      <title>Book example</title>
    </head>
    
    <body>
      {{> insertBookForm}}
    </body>
    
    <template name="insertBookForm">
      {{> quickForm collection="Books" id="insertBookForm" type="insert"}}
    </template>
    

    【讨论】:

    • 嗨,是的,我已经仔细查看了文档。插入似乎不起作用。然后我查看了github.com/aldeed/meteor-collection2 包,这就是我从中获取插入功能的地方。
    • 我已经更新了我的答案,所以代码是完整的。也许你忘了放发布/订阅代码?但从你的另一个问题看来,你正在使用 autopublish 包。无论如何,上面的代码应该可以工作。你能准确地测试一下这段代码并报告回来吗?
    • 太好了,谢谢。我认为它仍然可以工作,因为我安装了自动发布和不安全的软件包。 Books.find().fetch(); [对象 { _id="BpjeYpbZzmYA7fe7J", title="title", author="author", 更多...}]
    • 如果您使用自动发布包,我实际上假设它应该在没有发布/订阅逻辑的情况下工作。但是,如果您的问题得到解决,那就完美了!
    • 是的,解决了。我将更深入地研究订阅/发布功能,看看我是否能更深入地了解这一点。再次感谢.......下一步看看我是否可以使用 autoform 添加到用户个人资料中,祈祷! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 1970-01-01
    • 2016-06-08
    • 2015-06-29
    • 1970-01-01
    • 2014-06-03
    • 2016-02-13
    相关资源
    最近更新 更多