我的流星应用程序有一个非常奇怪的问题,我无法弄明白。我正在使用Meteor v1.3和NodeJS v0.10.42。我只是试图获取HTML表单的文本并通过电子邮件发送数据。使用meteor
命令在本地计算机上运行时,它可以正常工作,但是当我执行meteor build
并将其部署到我的服务器时,它将不再起作用。我在我的代码中留下了我的console.logs,以显示它在哪里中断。
HTML表单:
<form id="contactForm">
Name:
<input type="text" id="name" placeholder="Name" autocorrect="off" autocapitalize="words">
Email:
<input type="email" id="email" placeholder="Email">
Message:
<textarea id="message"></textarea>
<input type="submit" value="Send">
</form>
客户代码:
Template.contact.events({
'submit form#contactForm':function(e){
console.log("Submit Button Clicked");
var name = e.target.name.value;
var email = e.target.email.value;
var message = e.target.message.value;
var namel = name.length;
var emaill = email.length;
var messagel = message.length;
if(namel > 0 && emaill > 0 && messagel > 0){
var text = "Message from: " + name + " \rEmail: " + email + "\rContent:" + message;
Meteor.call('sendEmail', text);
console.log("Just called sendEmail method");
alert('Message sent! Thank you', 'success');
}else{
console.log("Not all fields were filled out, and the form wasn't submitted.");
alert('An error occurred. Please make sure you have filled in all the fields.', 'error');
return false;
}
}
});
服务器代码:
'sendEmail': function (text) {
this.unblock();
Email.send({
to: 'email@test.com',
from: 'anotheremail@test.com',
subject: 'Form Submission',
text: text
});
}
当我提交表单并将字段留空时,它会记录所有console.log
条消息。但是,如果我提交表单的所有部分,它只会记录“提交按钮单击”。我知道IF
语句可以正常工作,因为它在网页中正确提醒。我也知道服务器方法工作正常,因为我可以从其他任何地方调用它。如果我在客户端代码的else
中调用它,它也可以工作。它还可以从表单中正确捕获数据,因为我可以console.log
这些变量并且它们正确显示。我真的很困惑为什么它在我的本地完美运行但在服务器上根本不起作用。
答案 0 :(得分:0)
客户代码:
Template.contact.events({
'submit form#contactForm':function(e){
console.log("Submit Button Clicked");
var name = e.target.name.value;
var email = e.target.email.value;
var message = e.target.message.value;
var namel = name.length;
var emaill = email.length;
var messagel = message.length;
if(namel > 0 && emaill > 0 && messagel > 0){
var text = "Message from: " + name + " \rEmail: " + email + "\rContent:" + message;
Meteor.call('sendEmail', text);
console.log("Just called sendEmail method");
alert('Message sent! Thank you', 'success');
e.currentTarget.reset();
return false;
}else{
console.log("Not all fields were filled out, and the form wasn't submitted.");
alert('An error occurred. Please make sure you have filled in all the fields.', 'error');
return false;
}
}
});