not-an-aardvark / snoowrap

A JavaScript wrapper for the reddit API
MIT License
1.02k stars 127 forks source link

Fetching replies to brand-new comments throws an error #305

Open Chaphasilor opened 3 years ago

Chaphasilor commented 3 years ago

Today I encountered some strange error messages:

When there's a brand new comment (less than ~30s old), and I try to fetch the replies to that comment, I get the following error message:

an error occured checking for mentions: TypeError: Cannot read property 'constructor' of undefined
    at getEmptyRepliesListing (D:\Code\NodeJS\odcrawler-scanner\node_modules\snoowrap\dist\helpers.js:43:12)
    at addEmptyRepliesListing (D:\Code\NodeJS\odcrawler-scanner\node_modules\snoowrap\dist\helpers.js:74:18)
    at Proxy._transformApiResponse (D:\Code\NodeJS\odcrawler-scanner\node_modules\snoowrap\dist\objects\Comment.js:53:48)
    at D:\Code\NodeJS\odcrawler-scanner\node_modules\snoowrap\dist\objects\RedditContent.js:79:22
    at tryCatcher (D:\Code\NodeJS\odcrawler-scanner\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (D:\Code\NodeJS\odcrawler-scanner\node_modules\bluebird\js\release\promise.js:547:31)
    at Promise._settlePromise (D:\Code\NodeJS\odcrawler-scanner\node_modules\bluebird\js\release\promise.js:604:18)
    at Promise._settlePromise0 (D:\Code\NodeJS\odcrawler-scanner\node_modules\bluebird\js\release\promise.js:649:10)
    at Promise._settlePromises (D:\Code\NodeJS\odcrawler-scanner\node_modules\bluebird\js\release\promise.js:729:18)
    at _drainQueueStep (D:\Code\NodeJS\odcrawler-scanner\node_modules\bluebird\js\release\async.js:93:12)
    at _drainQueue (D:\Code\NodeJS\odcrawler-scanner\node_modules\bluebird\js\release\async.js:86:9)
    at Async._drainQueues (D:\Code\NodeJS\odcrawler-scanner\node_modules\bluebird\js\release\async.js:102:5)
    at Immediate.Async.drainQueues [as _onImmediate] (D:\Code\NodeJS\odcrawler-scanner\node_modules\bluebird\js\release\async.js:15:14)    at processImmediate (internal/timers.js:461:21)

It seems to be caused by fetching the replies, although it's hard to pinpoint the exact issue because there's not a single line of my own code in the stack trace. But poking around with the debugger also indicated that the issue has to do with accessing replies.

Here's what I'd call the 'relevant code':

try {
  // comment.replies = await comment.replies.fetchAll();
  comment = await comment.expandReplies(Infinity);
  if (comment.replies.length > 0) {
    return comment.replies.reduce((ret, curr) => {
      return ret || curr.author.name == this.username;
    }, false);
  } else {
    throw 'No comments found';
  }
} catch (err) {
  console.log('err:', err);
  return false;
}

As indicated by the error message, the 'comments' I'm working with are mentions fetched from the inbox. Might be relevant, might not be...

Chaphasilor commented 3 years ago

I just looked at this again and managed to reduce the code to the following:

  let mentions = (await this.client.getInbox({
    filter: `mentions`
  }))

  mentions.forEach(async x => {
    x = await x.expandReplies(Infinity);
  })

Steps to reproduce:

  1. Have the above code run every ~3 seconds on loop (just to be sure). setInterval() works just fine.
  2. Mention the bot's name inside a reddit comment.
  3. The bot should load the new mention after a few seconds and the next thing that happens is the error in my first message (about 'constructor' of undefined.

I never got this error before and didn't change this part of the code for a few weeks. Maybe reddit changed their API a bit?

Chaphasilor commented 3 years ago

@SpyTec can you reproduce this? :)