发送电子邮件后会话变量未设置
Session variable unset after sending email
我想发送邮件并显示相关通知(以下是提交功能代码):
Session.set('alertMessage', ['Email has been sent', 'success']);
var a = Session.get('alertMessage');
console.log(a);
Meteor.call('sendEmail', dataText);
我的 SendEmail 方法:
Meteor.methods({
sendEmail: function (text) {
check([text], [String]);
this.unblock();
Email.send({
to: 'myEmail@email.xyz',
from: 'contactForm@email.xyz',
subject: 'New message from contact form',
text: text
});
}
});
并呈现联系人表单模板的回调:
Template.contactFormTemplate.rendered = function(){
var b = Session.get('alertMessage');
console.log(b);
}
问题:我在控制台中看到 a
变量正确获取会话值:
数组['Email has been sent', 'success']
但是在页面重新加载后(提交表单时)b
变量无法获取会话变量:
undefined
即使我尝试从控制台获取此会话变量:
var c = Session.get('alertMessage')
console.log(c)
它 returns undefined
- 谁能帮我解决这个问题?
最后,我设法自己解决了这个问题 - 这是表单提交操作代码:
Session.set('alertMessage', ['Message has been sent.', 'success']);
Meteor.call('sendEmail', dataText);
并呈现回调的联系表单模板:
Meteor.defer(function(){
var a = Session.get('alertMessage');
if(a){
throwAlert(a[0], a[1]); //my custom method for nicer alert popups
Session.set('alertMessage', null);
}
});
最后,我不得不添加 persistent-session 包:
meteor add u2622:persistent-session
我想发送邮件并显示相关通知(以下是提交功能代码):
Session.set('alertMessage', ['Email has been sent', 'success']);
var a = Session.get('alertMessage');
console.log(a);
Meteor.call('sendEmail', dataText);
我的 SendEmail 方法:
Meteor.methods({
sendEmail: function (text) {
check([text], [String]);
this.unblock();
Email.send({
to: 'myEmail@email.xyz',
from: 'contactForm@email.xyz',
subject: 'New message from contact form',
text: text
});
}
});
并呈现联系人表单模板的回调:
Template.contactFormTemplate.rendered = function(){
var b = Session.get('alertMessage');
console.log(b);
}
问题:我在控制台中看到 a
变量正确获取会话值:
数组['Email has been sent', 'success']
但是在页面重新加载后(提交表单时)b
变量无法获取会话变量:
undefined
即使我尝试从控制台获取此会话变量:
var c = Session.get('alertMessage')
console.log(c)
它 returns undefined
- 谁能帮我解决这个问题?
最后,我设法自己解决了这个问题 - 这是表单提交操作代码:
Session.set('alertMessage', ['Message has been sent.', 'success']);
Meteor.call('sendEmail', dataText);
并呈现回调的联系表单模板:
Meteor.defer(function(){
var a = Session.get('alertMessage');
if(a){
throwAlert(a[0], a[1]); //my custom method for nicer alert popups
Session.set('alertMessage', null);
}
});
最后,我不得不添加 persistent-session 包:
meteor add u2622:persistent-session