如何在没有autoform的情况下在Meteor中插入一个对象数组?

时间:2015-12-30 01:07:09

标签: arrays mongodb object meteor insert

我在这里使用的示例来自http://autoform.meteor.com/quickform

我一直在为一些项目使用autoform,但我需要在没有它的情况下获得相同的行为,我怎么能插入一个对象数组呢?

所以这里是autoform所需的Schema定义

items
  .groupBy(x => x.type)
  .subscribe(obs => obs.map(function(value, index){
                               return index == 0 ? process_first(value)
                                                 : process_others(value)
                            })
                       .subscribe(x => console.log('sub == ', x))

接下来是调用autoform来生成模板中的表单

 items: {
  type: Array,
  optional: true,
  minCount: 0,
  maxCount: 5
 },
   "items.$": {
      type: Object
   },
   "items.$.name": {
      type: String
   },
   "items.$.quantity": {
      type: Number
   }

有了这个,您就会得到一个表单,显示两个字段:名称和数量,以及用这些相同字段表示更多对象的符号,当您实际提交表单时,它会插入所有对象。

HTML和CSS不是问题

1 个答案:

答案 0 :(得分:1)

我不太确定你在问什么。这是将数组插入集合的两种方法:

// insert items one by one
// NOTE: Meteor doesn't allow inserting of arrays like `Items.insert(items)`
items.forEach(item => Items.insert(item));

// insert all items simultaneously by using raw mongo collection:
Items.rawCollection().insert(items);