【问题标题】:Meteor/MongoDB Duplicate Array ObjectMeteor/MongoDB 重复数组对象
【发布时间】:2017-01-13 23:00:52
【问题描述】:

我很好奇最好的方法是在流星中创建一个按钮来复制一个数组对象。例如,如果我有:

"TasksWithData": [
  {
    "inspectionDetails": [
      {
        "name": "somename",
        "inspector": "Ss6TjGGzqWJRZYStx",
        "inspectorName": "name of inspector",
        "projectType": "inspection",
        "startDate": "2017-01-12T05:00:00.000Z",
        "desc": "a description",
        "activeTask": true
      }
    ],
    "TaskItem": [
      {
        "DataFacilitySection": "dsfgsdfgds",
        "DataItemDescription": "item 2",
        "DataItemSection": "dfgdfgdf",
        "DataItemCode": "dfgdfgdf",
        "DataItemPass": null
      }
    ],
  }
],

如果我想使用客户端按钮复制整个 TasksWithData 数组(这是一个子文档并且没有 _id)...我该怎么做?

这是我要调用的事件:

  'click .duplicate': function(){
    Meteor.call('duplicateItem', this._id);
  }

这是模板结构:

检查/AddInspectionsHome:

<template name="AddInspectionHome">
  <div class="container single-list conatiner-padding">
    <h1>Task Navigator</h1>
    <hr>
      <div class="panel panel-default">
        <div class="panel-heading no-padding">
          <span>Select a Client</span>
        </div>
        {{#each findClients}}
          {{> SingleClientInspection}}
        {{/each}}
      </div>
  </div>
  <!--TODO: Need to add the ability to duplicate these inspections-->
</template>

Inspections/SingleClientAddInspection(触发用户查看活动检查的 ReactiveVar 模式):

{{#if activeMode}}
    <div class="panel panel-default col-lg-12">
      <a href="#" class="active btn btn-primary">Close</a>
      <div class="panel panel-default">
        <div class="panel-heading">View Active Tasks</div>
          <table class="table table-striped">
            <thead>
              <tr>
                <th>Facility</th>
                <th>Inspector</th>
                <th>Type</th>
                <th>Due Date</th>
              </tr>
            </thead>
            <tbody>
              {{#each TasksWithData}}
                {{#each inspectionDetails}}
                  {{#if activeTask}}
                    <tr>
                      <td>{{TaskFacility}}</td>
                      <td>{{inspectorName}}</td>
                      <td>{{projectType}}</td>
                      <td>{{startDate}}</td>
                      <td><a href="#" class="duplicate btn btn-success">Copy</a></td>
                    </tr>
                  {{/if}}
                {{/each}}
              {{/each}}
            </tbody>
          </table>
      </div>
    </div>
  {{/if}}

【问题讨论】:

  • 复制到哪里?到另一个 mongo 文件?到临时变量?到同一个 mongo 文档中的另一个键?
  • 实际上,我真的很想将它作为一个新的相同对象添加到同一个数组中。客户希望能够复制先前设置的检查,这样他们就不必从头开始重新添加它们。所有这些检查确实需要像上面一样存在于同一个数组中。
  • 我也编辑了上面的代码。我的错。我忘了关闭父对象。

标签: javascript mongodb meteor


【解决方案1】:

我建议先提取原始数组,然后将其连接回自身。

const doc = MyCollection.findOne(_id); // however you get your original doc
let TasksWithData = doc.TasksWithData;
TasksWithData.concat(TasksWithData);
MyCollection.update(_id,{$set: {TasksWithData: TasksWithData}});

【讨论】:

  • 非常感谢,我将尝试将其作为与单击事件相关的方法。明天再来汇报!感谢您抽出宝贵时间!
  • 嗯,这感觉就像接近了,但我有一些问题需要解决。我会更新我原来的帖子,看看你的想法!再次感谢您对此的帮助!
  • 由于您的事件处理程序在数组本身上运行,您可以使用Template.parentData(1)._id 获取父对象的_id
  • 感谢甜蜜的 7lb 6oz BABY JESUS!这正是我所缺乏的不仅仅是这种情况!我真的很喜欢 Blaze,并希望 MDG 找到一种方法来保留它,但 DOM 遍历(或在本例中为模板)一直是我的主要症结所在。我可以在这些文档中阅读更多内容吗?干杯兄弟,感谢您的努力!
  • 嘿@Michel 我实际上在使用这种方法时遇到了一个小问题。既然您非常擅长回应(而且我的截止日期很紧哈哈),您能否看看这个相关的问题? autofom update not working
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
  • 1970-01-01
  • 1970-01-01
  • 2018-08-23
  • 2015-05-12
相关资源
最近更新 更多