Autoform插入时出错

时间:2015-08-06 16:04:21

标签: meteor meteor-autoform

我遇到问题让我的Insert Autoform正常工作。我试图让它与示例http://autoform.meteor.com/insertaf类似,我的代码如下。我已经删除了不安全和自动发布

client/templates/venues/venue_submit.html

<template name="venueSubmit">
    <!-- {{> quickForm collection="Venues" id="venueSubmit" type="insert"}} -->
    {{#if isSuccessfulvenueSubmit }}
    <h2>Thanks for the Venue </h2>

    {{else}}
      {{#autoForm id="insertVenueForm" type="insert" collection=Collections.Venues omitFields="createdAt" resetOnSuccess=true}}

   {{> afQuickField name="Venue"}}

<div class="form-group">
        <button type="submit" class="btn btn-primary">Add Venue</button>
        <button type="reset" class="btn btn-default">Reset Form</button>
        </div>
      {{/autoForm}}
    {{/if}}
</template>


client/templates/venues/venue_submit.js
        Schemas = {};

    Template.registerHelper("Schemas", Schemas);

    Schemas.Venue = new SimpleSchema({
      Venue: {
        type: String,
        label: "Venue Name",
        max: 200,
        autoform: {
          placeholder: "Name of the Venue"
        }
      },
....
});
AutoForm.debug()

var Collections = {};

Template.registerHelper("Collections", Collections);

Venues = Collections.Venues = new Mongo.Collection("Venues");
Venues.attachSchema(Schemas.Venue);

Venues.allow({
  insert: function (userId, doc) {
    return true;
  },
  remove: function (userID, doc, fields, modifier) {
    return true;
  },
  remove: function (userId, doc) {
      return true;
  }
});

if (Meteor.isClient) {
    Meteor.subscribe("Venues")
};

server/Publications.js
Meteor.publish('venue', function () {
  return Venues.find(id);
});

Error Message I get

2 个答案:

答案 0 :(得分:0)

AutoForm insert类型生成文档并插入客户端。如果未安装autopublishinsecure软件包,则需要确保在客户端上订阅相应的集合。如果您不订阅它,它就不存在。

if (Meteor.isClient) {
    Meteor.subscribe("Venues")
}

答案 1 :(得分:0)

您的问题是您在客户端文件夹中有venue_submit.js,但其内容并非仅供客户使用。

您可以完全保留venue_submit.html。 将venue_submit.js更改为:

Template.registerHelper("Schemas", Schemas);
AutoForm.debug();
Template.registerHelper("Collections", Collections);
Meteor.subscribe("Venues");

您只需要与客户端相关的行:两个模板助手,AutoForm调试(除了调试之外您不需要),以及订阅Venues集合。

更改server / Publications.js以包含与服务器端相关的所有内容:

Meteor.publish('Venues', function () {
  return Venues.find({});
});

Venues.allow({
  insert: function (userId, doc) {
    return true;
  },
  remove: function (userID, doc, fields, modifier) {
    return true;
  },
  remove: function (userId, doc) {
      return true;
  }
});

它包括发布功能和集合的权限。

现在创建lib / schema.js:

Schemas = {};

Schemas.Venue = new SimpleSchema({
      Venue: {
        type: String,
        label: "Venue Name",
        max: 200,
        autoform: {
          placeholder: "Name of the Venue"
        }
      }
});

Collections = {};

Venues = Collections.Venues = new Mongo.Collection("Venues");
Venues.attachSchema(Schemas.Venue);

lib中的所有内容都可供客户端和服务器使用,并且将首先加载此文件夹的内容,以便架构和集合定义可供所有其余代码使用。请注意,集合中缺少var关键字。使用var将范围设置为仅在文件中。省略它会使整个代码中的Collections变量可用。

相关问题