question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fetch.on("message") event is firing twice, though imap.search result is only one result

See original GitHub issue

I am trying to fetch all unseen messages, but some times mail event is triggering twice

var self = this, search = self.options.search || [[ 'UNSEEN', ['SINCE', 'May 20, 2010'] ]];
    self.dbg('scanning %s with filter `%s`.', self.options.box,  search);
    self.imap.search(search, function (err, seachResults) {
        if (err) {
            self.emit('error', err);
            callback();
            return;
        }
        if (!seachResults || seachResults.length === 0) {
            self.dbg('no new mail in %s', self.options.box);
            callback();
            return;
        }
        self.dbg('found %d new messages', seachResults.length);
        var fetch = self.imap.fetch(seachResults, {
            markSeen: self.options.markSeen !== false,
            bodies: ''
        });
//this below event emitting twice
        fetch.on('message' function (msg,seqno) {
            var uid, flags;
            msg.on('attributes', function(attrs) {                                                           
                uid = attrs.uid;
                flags = attrs.flags;
                **self.dbg("Message uid", attrs.uid);                                                               
            }); 
            var mp = new MailParser();
            mp.once('end', function (mail) {
                mail.uid = uid;
                mail.flags = flags;
                if(uid>last_seq)
                self.emit('mail', mail);
                self.dbg('found mail '+mail.headers["message-id"]);
            });
            msg.once('body', function (stream, info) {
                stream.pipe(mp);
            });
        });
        fetch.once('end', function () {
            self.dbg('Done fetching all messages!');
            callback();
        });
```   The logs are showing mail id/uid printed twice but scanned only once and scan result was 1 item
This is not always happening, any solution so that fetch.on message only triggered once for once search result

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mscdexcommented, Nov 27, 2019

As far as the search criteria goes, I think there is an extra set of brackets that need to be removed, it should be just [ 'UNSEEN', ['SINCE', formattedDate] ] that is passed to search().

1reaction
mscdexcommented, Nov 26, 2019

As far as the error goes, that’s coming from mailparser, not imap, so you’d want to submit an issue with that project instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why are these two IMAP Search results different from one ...
One ( SEARCH ) returns message sequence numbers (MSN), which are numbered from 1 to N and change as messages are added and...
Read more >
Outlook search - duplicate emails for gmail imap
I'm using a gmail imap account with Outlook for Mac v15.33. ... Outlook for Mac client, it will show duplicated emails in the...
Read more >
Common Problems with Google Calendar - Zapier Help
The "New Event Matching Search" trigger is firing too often or on old events. If older events in Google Calendar are updated, this...
Read more >
JMAP Mail Specification
If a JMAP Mail server also provides an IMAP interface to the data and supports ... If true , when sorting the query...
Read more >
When attempting to copy or move large numbers of local ...
As a result, half the messages never appeared on the IMAP server but did disappear ... I tried to look through it to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found