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

标签 javascript meteor meteor-autoform

我正在尝试设置一个用于创建用户的表单。架构的电子邮件部分设置为数组:

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

我使用自动表单在模板中创建表单:

    {{#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}));
    }
});

但是尝试查找 emails.0.address 返回错误:

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

最佳答案

做错的事情太多了。请仔细阅读简单模式和自动格式自述文件。这是一个非常强大的包,确实让很多事情变得更容易。

  1. 您的架构。 仅当字段类型为 Object 时才使用 blackbox: true

  2. 您的自动表单。 您已指定两个 collection="Users" schema=schema,请仅指定一个。

  3. 您的表单处理。 使用 AutoForm.hooks 而不是自定义事件监听器。

    AutoForm.hooks({ 添加用户: { onSubmit:功能(文档){ console.log( doc.emais[0].address ); console.log( 文档.用户名 ); 返回真; } } });

关于javascript - Meteor 从模式定义的表单中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30673480/

相关文章:

Meteor.Error 不显示任何内容

javascript - 无法在 Template.onRendered() Meteor 上获取 div 的高度

javascript - 自动成型插入 : doc value isn't having any effect

javascript - 在 Meteor.js 中设置自动表单中省略字段的值

javascript - 如何将 .innerHTML 转换为 PHP 变量

javascript - 单击外部链接时的标记位置

Meteorjs 模型/集合可在客户端访问。安全问题?

javascript - meteor-autoform 无法显示表单 : Cannot read property 'schema' of undefined

javascript - 如何访问Knockout组件中的viewModel?

javascript - 在不同的标签上正确触发 JavaScript