Not sure why there is getTime no method error below? And when submitting two email sends, I got tons of emails from running it when only two emails are expected 20 and 40 secs apart?
var moment =require(‘moment’);
Meteor.startup(() => {
process.env.MAIL_URL=‘smtp://USERNAME@gmail.com:PASSWORD@smtp.gmail.com:465/’;
var details = {
from: ‘username@gmail.com’,
to: username@gmail.com’,
subject: ‘First Message Subject’,
text: ‘First Text’,
date: moment().add(10, ‘seconds’).unix()
};
var details2 = {
from: ‘username@gmail.com’,
to: ‘username@gmail.com’,
subject: ‘Second Message Subject’,
text: ‘Second Text’,
date: moment().add(20, ‘seconds’).unix()
};
console.log(‘details.date is now :’ ,details.date);
console.log(‘details2.date is now :’ ,details2.date);
FutureTasks.find().forEach(function (mail) {
if (mail.date < moment().unix()) {
Meteor.call(‘sendEmail’, mail);
} else {
// addTask(mail._id, mail);
Meteor.call(‘addTask’,mail._id, mail);
}
});
SyncedCron.start();
Meteor.call(‘scheduleMail’, details);
Meteor.call(‘scheduleMail’, details2);
}); //end of Meteor.startup
Meteor.methods({
sendEmail: function (details) {
check(details, Object);
// Let other methods calls from the same client start running,
// without waiting for the email sending to complete.
this.unblock();
Email.send({
to: details.to,
from: details.from,
subject: details.subject,
text: details.text,
date: details.date
}); // end of Email model
}, // end of sendEmail definition
addTask: function (id, details) {
SyncedCron.add({
name: id,
schedule: function (parser) {
return parser.recur().on(details.date).fullDate(); <- THROWS NO METHOD 'getTime' ERROR ????
},
job: function () {
Meteor.call('sendEmail', details);
FutureTasks.remove(id);
SyncedCron.remove(id);
return id;
}
});
},
scheduleMail: function (details) {
if (details.date < moment().unix()) {
Meteor.call('sendEmail', details);
} else {
var thisId = FutureTasks.insert(details);
Meteor.call('addTask',thisId, details);
}
return true;
}
});// end of Meteor.methods