无法将关系 ID 插入到 Aldeed 的 Autoform MeteorJS 框架中 [英] Cannot insert relationship ID into Aldeed's Autoform MeteorJS framework

查看:29
本文介绍了无法将关系 ID 插入到 Aldeed 的 Autoform MeteorJS 框架中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在尝试将氏族数据插入到武士身份中.我好像做不到.

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

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

为观看乐趣而制作的随机 Github

https://github.com/qtheninja/SamuraiAttack

https://github.com/aldeed/meteor-collection2/issues/31如何在 Meteor 中添加与 AutoForm 的关系或引用?带有collection2的Meteor Autoform包不提交表单

//lib/collections/clans.js

 Clans = new Mongo.Collection('clans');Clans.attachSchema(new SimpleSchema({名称: {类型:字符串,label: "氏族名称",最大:100}}));如果(流星.isServer){氏族.允许({插入:函数(用户 ID,文档){返回真;},更新:函数(用户 ID、文档、字段名称、修饰符){返回真;},删除:功能(用户ID,文档){返回真;}});}

//lib/collections/samurais.js

 Samurais = new Mongo.Collection('samurais');Samurais.attachSchema(new SimpleSchema({标题: {类型:字符串,标签:标题",最大:100},描述: {类型:字符串,标签:描述",可选:真},氏族:{类型:部落}}));如果(流星.isServer){Samurais.allow({插入:函数(用户 ID,文档){返回真;},更新:函数(用户 ID、文档、字段名称、修饰符){返回真;},删除:功能(用户ID,文档){返回真;}});}

//client/template/clans/createClan.html

 <模板名称="CreateClan"><h1>创建新氏族</h1>{{>快速表格集合=氏族"id="插入氏族表格"类型=插入"buttonContent="创建"}}<div>{{>列表族 }}

//client/main.js

 AutoForm.addHooks('insertSamuraiForm', {前: {插入:函数(文档,模板){//在这里修改文档doc.clanid = 45;doc.dance ="重新开始";console.log("钩子后运行");返回真;}}});AutoForm.hooks({插入武士表格:{前: {插入:函数(文档,模板){//在这里修改文档doc.projectid="随机";doc.dance ="重新开始";console.log('这是第二种形式');}}}});

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

  1. 使用before"钩子返回对象
  2. Router.current().params._id一种.我正在使用铁路由器,在我的网址中是氏族/_id/samurai/new
  3. 添加了 'dance' 和 'clanid' 作为 simpleschema 的一部分.我忽略了将它们作为架构的一部分,所以我让 console.log 工作,但不是将数据与对象分开.

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

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

//这里修改文档doc.clanid= Router.current().params._id;doc.dance ="重新开始";console.log("钩子后运行");返回文档;

}}

//lib/collections/samurais.js

Samurais = new Mongo.Collection('samurais');Samurais.attachSchema(new SimpleSchema({标题: {类型:字符串,标签:标题",最大:100},描述: {类型:字符串,标签:描述",可选:真},氏族:{类型:字符串,标签:忽略这个",可选:真},舞蹈: {类型:字符串,可选:真}}));如果(流星.isServer){Samurais.allow({插入:函数(用户 ID,文档){返回真;},更新:函数(用户 ID、文档、字段名称、修饰符){返回真;},删除:功能(用户ID,文档){返回真;}});}

New to MeteorJS. I started making a novel Clan/Samurai app to see if I could understand how mongo/meteor and Autoforms handle relationships. I'm trying to make clans and samurai relate to each other so that each Samurai has a specific clan.

I'm attempting to insert the clan data into the Samurai identity. I seem to not be able to.

I've read the following and still seem generally confused on how to implement this. I've tried before, onsuccess, onsubmit hooks. I've attempted to set the schema up so that it works. I mostly get AutoForm undefined or Schema undefined...tons of errors it seems. I've read that it should be client.

I can console log and get it to render but I can't add the new items to the collection.

Random Github made for viewing pleasure

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');
      }
      }
    }
  });

解决方案

I was able to resolve this issue by doing the following.

  1. Return on object using 'before' hook
  2. Router.current().params._id a. I was using iron router and in my url was clans/_id/samurai/new
  3. added 'dance' and 'clanid' as apart of the simpleschema. I had neglected to include them as apart of the schema so I was getting the console.log to work but not the data to be apart of the object.

//client/lib/main.js (alterations)

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

//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;
    }
  });
}

这篇关于无法将关系 ID 插入到 Aldeed 的 Autoform MeteorJS 框架中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆