此代码从表单中提取字段值,我可以在控制台中调试时看到正确传递的值。但是,由于额外的对象添加了我的第一个值,Meteor会抛出一个消毒错误......任何想法?
Template.sendInvitationModal.events({
'submit form': function submitForm(event, template) {
event.preventDefault();
var firstName = template.find("[name='firstName']").value,
lastName = template.find("[name='lastName']").value,
email = template.find("[name='emailAddress']").value,
store = template.find("[name='store'] option:selected").value,
position = template.find("[name='position'] option:selected").value,
roles = template.find("[name='roles'] option:selected").value;
debugger;
if (email && roles && position !== "") {
Meteor.call("sendInvitation", {
firstName: firstName,
lastName: lastName,
email: email,
store: store,
position: position,
roles: roles
}, function (error, response) {
if (error) {
toastr["warning"]( error.reason);
} else {
$("#send-invitation-modal").modal('hide');
$('.modal-backdrop').hide();
toastr["success"]( "Invitation sent!" );
}
});
} else {
toastr["warning"]( "Please set an email and at least one user type!" );
}}});
这是在调用“sendInvitation”时使用firstName传递的值
firstName = "Richard", template = B…e.TemplateInstance {view: B…e.View, data: Object, firstNode: div#send-invitation-modal.modal.fade.in, lastNode: div#send-invitation-modal.modal.fade.in, _allSubsReadyDep: T…r.Dependency…}
然后Meteor抛出一个消毒错误。有什么建议吗?
答案 0 :(得分:0)
我们设法(感谢Ryan Glover的帮助)通过简单地将变量赋值从从表单中拉出的值转换为对象来找到解决方案。解决了!所以正确的代码是......
Template.sendInvitationModal.events({
'submit form': function submitForm(event, template) {
event.preventDefault();
var data = {
firstName: template.find("[name='firstName']").value,
lastName: template.find("[name='lastName']").value,
email: template.find("[name='emailAddress']").value,
store: template.find("[name='store'] option:selected").value,
position: template.find("[name='position'] option:selected").value,
roles: template.find("[name='roles'] option:selected").value
};
if (data.email && data.roles && data.position !== "") {
Meteor.call("sendInvitation", data, function (error, response) {
if (error) {
toastr["warning"]( error.reason);
} else {
$("#send-invitation-modal").modal('hide');
$('.modal-backdrop').hide();
toastr["success"]( "Invitation sent!" );
}
});
} else {
toastr["warning"]( "Please set an email and at least one user type!" );
}
}
});