Meteor从模式定义的表单中获取数据

Meteor get data from form that is defined by a schema

本文关键字:获取 数据 表单 模式 定义 Meteor      更新时间:2023-09-26

我正在尝试建立一个用于用户创建的表单。模式的email部分设置为一个数组:

...
"emails.$.address": {
    type: String,
    blackbox: true
},
"emails.$.verified": {
    type: Boolean,
    optional: true,
    blackbox: true
},
...

我使用auto form在模板内部创建表单:

    {{#autoForm id="addUser" type="method" meteormethod="createUserwRole" collection="Users" schema=schema resetOnSuccess="true" }}
    <fieldset>
        {{> afQuickField name="fName" id="fName"}}
        {{> afQuickField name="lName" id="lName"}}
        {{> afQuickField name="username" id="username"}}
        {{> afQuickField name="emails" id="emails"}}
        {{> afFormGroup name="roles" options=options firstoption="Select Role" type="select-multiple" id="roles"}}
    <div>
      <button type="submit" class="btn btn-primary" data-toggle="modal" data-target="#addUser">Submit</button>
      <button type="reset" class="btn btn-default">Reset</button>
    </div>
  </fieldset>
  {{/autoForm}}

然后在提交时,我想获取数据:

Template.addUser.events({
  'submit #addUser': function(e, t) {
    console.log("hit");
    console.log(t);
    e.preventDefault();
    var email = t.find("#emails.0.address").value;
    var username = t.find("#username").value;
    console.log(email);
        Meteor.call("createUserwRole", ({"email":email, "username":username}));
    }
});

但是试图找到电子邮件。Address返回错误:

TypeError: null is not an object (evaluating 't.find("#emails.0.address").value')

做错的事情太多了。请浏览简单模式和自动表单README文件。它是一个非常强大的软件包,确实使很多事情变得更容易。

  1. 你的模式。

  2. 仅当字段类型为Object时使用blackbox: true
  3. Autoform。

  4. 您指定了两个collection="Users"schema=schema,请只指定一个。
  5. 您的表单处理。使用AutoForm.hooks代替自定义事件监听器。

    AutoForm.hooks ({addUser: {onSubmit: function (doc) {console.log (doc.emais[0]。地址);console.log (doc。用户名);返回true;}}});