Meteor app不会将元素插入集合中

时间:2017-05-01 17:39:21

标签: javascript meteor meteor-blaze meteor-autoform meteor-collections

我正在学习Meteor JS并按照教程构建菜单构建器购物清单。我想尝试添加一些功能。我成功添加了一个功能,但现在我正在尝试创建一个组织功能,用户可以加入组织并查看与该组织相关的所有购物清单。第一步是允许用户添加组织。

表单出现了,我可以从控制台插入数据库,但是当我使用autoform时,对象没有插入到数据库中。

我最近从Meteor 1.3升级到1.4。我不相信这是一个问题,因为应用程序上的所有其他表单都正常插入。

我觉得它与订阅/发布有关,但我不确定我做错了什么。

HTML- neworganization.html

        <template name='NewOrganization'>
    <div class='new-organization-container'>

        <i class='fa fa-close'></i>

          {{#autoForm collection='Organizations' id='insertOrganizationForm'  type='insert'}}
<div class='form-group'>
      {{> afQuickField name='organization'}}
      </div>
      <div class='form-group'>
      {{> afQuickField name='members'}}
      </div>



    <button type="submit" class="btn btn-primary">Add</button>
  {{/autoForm}}

    </div>


</template>

organizations.html

<template name='Organizations'>
<h3>Your Organizations</h3>
{{#if $.Session.get 'newOrganization'}}
        {{> NewOrganization }}
{{else}}
<button class='btn btn-organization btn-primary'>Add an Organization</button>
<button class='btn btn-join'>Join an Organization</button>
<button class='btn btn-deny'>Leave an Organization</button>
{{/if}}
<section class='organization-list'>
        {{#if Template.subscriptionsReady}}
        {{#each organizationList}}
            {{> OrganizationItem}}
        {{/each}}
    {{else}}
        <p>Loading...</p>
    {{/if}}

JS- organizationss.js

Template.Organizations.onCreated(function() {
  this.autorun(() => {
    this.subscribe('organizations');
  });
});

Template.Organizations.helpers({
    organizations()  {
        return Organizations.find({});
    }
});


Template.Organizations.events({
      'click .btn-organization': () => {
        Session.set('newOrganization', true);
      }
});

Template.NewOrganization.helpers({
    organizationList: () => {

      var organizationItems = Organizations.find({});

        return organizationItems;
    }
});

newOrganization.js

    if (Meteor.isClient) {
    Meteor.subscribe('organizations');
} 
Template.NewOrganization.events ({
    'click .fa-close': function () {
        Session.set('newOrganization', false);
    }
});

集合/ organizations.js

    import SimpleSchema from 'simpl-schema';
SimpleSchema.extendOptions(['autoform']);


Organizations = new Mongo.Collection('organizations');

Organizations.allow({
    insert: function(userId){
        return !!userId;
    },
    update: function(userId, doc){
        return !!userId;
    }
});


OrganizationSchema = new SimpleSchema ({
    organization: {
        label: "Organization Name",
        type: String
    },
    id: {
        label: "ID",
        type: String,
        autoform: {
            type: "hidden"
        }

    },
    members: {
        type: Array
    },
        "members.$": Object,
        "members.$.name": String,
        "members.$.role": String,
          inOrganization: {
            type: Boolean,
            defaultValue: true,

            autoform: {
                type: 'hidden'
            }
      },

    createdAt: {
        type: Date,
        label: "CreatedAt",
        autoform: {
            type: "hidden"
        },
        autoValue: function() {
            return new Date();
        }
    }
});

Meteor.methods({
    deleteOrganizations: function(id) {
        Organizations.remove(id);
    }
});

Organizations.attachSchema(OrganizationSchema);

1 个答案:

答案 0 :(得分:0)

问题在于Schema的设计方式。我在模式中插入了一个id。我的理由是我希望有一种方法来添加和删除组织中的成员。我没有考虑到的是Mongo自动生成数据库对象的id,并且通过这种方式设计我的模式,我创建了一个冲突。我从架构中删除了ID并删除了问题。

以下是新的collections / organizations.js文件:

=IIF(Parameters!ParamExpandOrCollapse.Value="Expand",True,False)