【问题标题】:Cannot insert relationship ID into Aldeed's Autoform MeteorJS framework无法将关系 ID 插入 Aldeed 的 Autoform MeteorJS 框架
【发布时间】:2015-09-21 11:55:04
【问题描述】:

MeteorJS 新手。我开始制作一个新颖的 Clan/Samurai 应用程序,看看我是否能理解 mongo/meteor 和 Autoforms 是如何处理关系的。我试图让氏族和武士相互关联,以便每个武士都有一个特定的氏族。

我正在尝试将氏族数据插入武士身份。我好像不能。

我已阅读以下内容,但似乎仍然对如何实现这一点感到困惑。我之前尝试过,onsuccess,onsubmit 钩子。我试图设置架构以使其正常工作。我主要得到 AutoForm undefined 或 Schema undefined ......似乎有很多错误。我读过它应该是客户端。

我可以控制日志并让它呈现,但我无法将新项目添加到集合中。

随机 Github 为观看乐趣而制作

https://github.com/qtheninja/SamuraiAttack

https://github.com/aldeed/meteor-collection2/issues/31 How to add a relationship or reference with AutoForm in Meteor? Meteor Autoform package with collection2 does not submit the form

//lib/collections/clans.js

 Clans = new Mongo.Collection('clans');

    Clans.attachSchema(new SimpleSchema({
     name: {
     type: String,
     label: "Clan Name",
     max: 100
     }
    }));

    if (Meteor.isServer) {
     Clans.allow({
    insert: function (userId, doc) {
      return true;
    },

    update: function (userId, doc, fieldNames, modifier) {
      return true;
    },

      remove: function (userId, doc) {
       return true;
      }
     });
    }

//lib/collections/samurais.js

  Samurais = new Mongo.Collection('samurais');

    Samurais.attachSchema(new SimpleSchema({
     title: {
     type: String,
     label: "Title",
     max: 100
    },
     description: {
     type: String,
     label: "Description",
     optional: true
    },
     clan: {
      type: Clans
      }
    }));

  if (Meteor.isServer) {
  Samurais.allow({
    insert: function (userId, doc) {
      return true;
    },

    update: function (userId, doc, fieldNames, modifier) {
      return true;
    },

    remove: function (userId, doc) {
      return true;
     }
     });
    }

//client/template/clans/createClan.html

    <template name="CreateClan">
    <h1>Create New Clan</h1>
    {{> quickForm 
    collection="Clans"
    id="insertClanForm"
    type="insert" 
    buttonContent="Create"
    }}
     <div>
       {{> ListClans }}

  </div>
</template>

//client/main.js

     AutoForm.addHooks('insertSamuraiForm', {

      before: {
      insert: function(doc, template) {

        //modify the document here
                  doc.clanid= 45;
          doc.dance ="start again";
          console.log("running after hook");
          return true;
       }
       }

  });

     AutoForm.hooks({
  insertSamuraiForm: {
    before: {
      insert: function(doc, template) {
        //modify the document here
          doc.projectid= "random";
          doc.dance ="start again";
          console.log('this is asecond form');
      }
      }
    }
  });

【问题讨论】:

    标签: meteor meteor-autoform


    【解决方案1】:

    我能够通过执行以下操作来解决此问题。

    1. 使用“之前”挂钩返回对象
    2. Router.current().params._id 一种。我使用的是 Iron 路由器,在我的 url 中是 clans/_id/samurai/new
    3. 添加了“dance”和“clanid”作为 simpleschema 的一部分。我忽略了将它们作为架构的一部分包括在内,所以我让 console.log 工作,但不是对象的数据。

    //client/lib/main.js(修改)

    之前:{ 插入:函数(文档,模板){

    //modify the document here
      doc.clanid= Router.current().params._id;
      doc.dance ="start again";
      console.log("running after hook");
      return doc;
    

    } }

    //lib/collections/samurais.js

    Samurais = new Mongo.Collection('samurais');
    
    Samurais.attachSchema(new SimpleSchema({
      title: {
        type: String,
        label: "Title",
        max: 100
      },
      description: {
        type: String,
        label: "Description",
        optional: true
      },
      clanid: {
        type: String,
        label: "ignore this",
        optional: true
    
      },
      dance: {
        type: String,
        optional: true
    
      }
    }));
    
    if (Meteor.isServer) {
      Samurais.allow({
        insert: function (userId, doc) {
          return true;
        },
    
        update: function (userId, doc, fieldNames, modifier) {
          return true;
        },
    
        remove: function (userId, doc) {
          return true;
        }
      });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-05
      • 2016-05-06
      • 2017-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      • 1970-01-01
      相关资源
      最近更新 更多