我正在尝试设置一个用于创建用户的表单。架构的电子邮件部分设置为数组:
...
"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')
最佳答案
做错的事情太多了。请仔细阅读简单模式和自动格式自述文件。这是一个非常强大的包,确实让很多事情变得更容易。
您的架构。 仅当字段类型为
Object
时才使用blackbox: true
。您的自动表单。 您已指定两个
collection="Users"
schema=schema
,请仅指定一个。您的表单处理。 使用
AutoForm.hooks
而不是自定义事件监听器。AutoForm.hooks({ 添加用户: { onSubmit:功能(文档){ console.log( doc.emais[0].address ); console.log( 文档.用户名 ); 返回真; } } });
关于javascript - Meteor 从模式定义的表单中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30673480/