【问题标题】:Error in Autoform insert自动插入错误
【发布时间】:2015-08-11 09:22:48
【问题描述】:

我在让插入自动表单正常工作时遇到问题。我试图让它类似于示例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);
});

【问题讨论】:

  • 我认为您没有显示足够的代码来复制此内容。请参阅:stackoverflow.com/help/mcve。请在场地字段中显示afFieldInput。您的代码对我来说很好,但我必须填写空白。您的问题可能就在这些空白处。
  • 我刚刚添加了它,我认为问题是我不确定将文件放在哪里。我在上面添加了他们的位置。

标签: meteor meteor-autoform


【解决方案1】:

AutoForm insert 类型生成一个文档并插入到客户端。如果没有安装 autopublishinsecure 软件包,您需要确保订阅客户端上的适当集合。不订阅就不存在。

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

【讨论】:

  • 如果我的代码设置与示例类似,那会不会放在 place_submit.js 文件的底部?
  • 是的,只要该代码在客户端上执行,应该可以工作。
  • 我在允许语句之后添加了它,但是当我尝试实现它时仍然遇到同样的问题。还有其他建议吗?
  • 每个文件都在一个共享的客户端/服务器目录中?
  • 没有前两个是客户端,发布是服务器端。我不确定如何拆分文件以共享它们。我试图将前两个文件放在 lib 文件夹中,但我得到了更多与未定义模板有关的错误。
【解决方案2】:

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

您可以原样保留venue_submit.html。 将 place_submit.js 更改为:

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

您只需要此处与客户端相关的行:两个模板助手、AutoForm 调试(除了调试之外您不需要)和订阅场地集合。

更改 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 变量在整个代码中可用。

【讨论】:

  • 非常感谢您。如果我添加表单提交钩子,这些钩子仍然会放在 place_submit.js 文件中,对吗?最后,如果我在 Mongo 中运行 db.venues.find(),在提交表单后,它不会返回任何内容。如果我在服务器文件上添加 Venues.find().fetch() ,它将在基本终端上打印出来。有问题吗?
  • 我更正了 Meteor.publish 函数。您使用名称venue 发布它,然后订阅Venues。这些值需要匹配,所以我将发布方法更改为发布为Venues。我还更改了它返回的内容,以便它返回整个集合,以便您可以在客户端看到它。在 mongo 方面,您的收藏被命名为场地,而不是场所(请参阅:Mongo.Collection("Venues"))。所以你应该使用db.Venues.find()。您可以通过在数据库中运行 show collections 看到这一点。至于表单提交钩子,是的,仍然在venue_submit.js中
  • 这解决了所有问题,但是返回整个集合是否会使数据库不安全?
  • @jaysig:如果您查看您引用的示例 (autoform.meteor.com/insertaf),则会返回整个集合。事实上,发布在发布功能中没有名称,因此可供所有客户端使用。它是否不安全取决于您要执行的操作。如果所有用户都可以查看所有场所,那么这并不是不安全的。如果您的 Venue 架构有一个“所有者”字段,并且您只希望用户看到他们拥有的场所,那么您可以编辑发布功能以说 return Venues.find({owner: this.userId});
猜你喜欢
  • 2016-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多