Javascript 如果没有结果,请正确终止IMAP节点
如果没有符合搜索条件的电子邮件,我将尝试退出脚本。如果我不执行Javascript 如果没有结果,请正确终止IMAP节点,javascript,node.js,imap,Javascript,Node.js,Imap,如果没有符合搜索条件的电子邮件,我将尝试退出脚本。如果我不执行imap.end(),它永远不会终止。添加imap.end时,会得到一个ECONNRESET。提前退出IMAP的正确方法是什么 function openInbox(cb) { imap.openBox('[Gmail]/All Mail', true, cb); } imap.once('ready', function() { openInbox(function(err, box) { if (err) thr
imap.end()
,它永远不会终止。添加imap.end时,会得到一个ECONNRESET
。提前退出IMAP的正确方法是什么
function openInbox(cb) {
imap.openBox('[Gmail]/All Mail', true, cb);
}
imap.once('ready', function() {
openInbox(function(err, box) {
if (err) throw err;
const aDayAgo = moment().subtract(1,'d');
imap.search([ ['SUBJECT', DPR_SUBJECT], ['SINCE', aDayAgo] ], function(err, results) {
if (err) throw err;
if (results.length == 0){
logging.log("No mail");
db.disconnect();
imap.end();
return;
}
var f = imap.fetch(results, {
bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)',
struct: true
});
f.on('message', function(msg, seqno) {
logging.verbose('Message #%d', seqno);
msg.once('end', function() {
logging.verbose(prefix + 'Finished');
});
});
f.once('error', function(err) {
logging.verbose('Fetch error: ' + err);
});
f.once('end', function() {
logging.log('Done fetching all messages!');
db.disconnect();
imap.end();
});
});
});
});
imap.once('error', function(err) {
logging.error(err.stack);
});
imap.once('end', function() {
logging.verbose('Connection ended');
});
exports.checkForMail = () => {
return imap.connect();
}