AlexAtHome / random-reddit

Random Reddit posts and images
MIT License
5 stars 1 forks source link

TypeError: Cannot read property 'data' of undefined error #5

Closed TheCuriousGoose closed 3 years ago

TheCuriousGoose commented 3 years ago

I've been using this package for my discord bot and for some subreddits ive been having this error

TypeError: Cannot read property 'data' of undefined
    at Object.getPost (C:\Users\Justin\Desktop\Code\Waterstream\node_modules\random-reddit\dist\functions.js:15:44)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async getImage (C:\Users\Justin\Desktop\Code\Waterstream\node_modules\random-reddit\dist\functions.js:27:16)
    at async Object.exports.run (C:\Users\Justin\Desktop\Code\Waterstream\commands\hentai.js:12:27)

using this code

var subreddits = subslist[Math.floor(Math.random() * subslist.length)];

const image = await getImage(subreddits).catch(e => {message.channel.send('Something went wrong.'); console.log(subreddits); return;})

if(image === null) return;
if(image.includes('https://redgifs.com/')) return message.channel.send(image).catch(e => { return })
AlexAtHome commented 3 years ago

If the issue still persists - let me know

TheCuriousGoose commented 3 years ago

The error still is there when a subreddit for example ecchi is requested

AlexAtHome commented 3 years ago

@TheCuriousGoose, I've found where was the problem and fixed it. Now posts from r/ecchi are fetched correctly.

Please, update to 2.0.2-0 and check if the problem is still here.

npm install random-reddit@2.0.2-0
ex1tium commented 3 years ago

I've had this issue too with certain subreddits they crash my irc bot. Brb testing 2.0.2.

ex1tium commented 3 years ago

I still have this issue. I deleted node modules. Updated package.json "random-reddit": "^2.0.2-0",. Then ran yarn install. Went to node_modules to check random-reddit/package.json and it says "version": "2.0.2-0",.

Trying to query forementioned ecchi throws this error:

HTTP GET https://reddit.com//r/ecchi/random.json?limit=1                                                                                                                                                                                                                                   14:24:50
HTTP Successful GET /r/ecchi/random.json?limit=1                                                                                                                                                                                                                                           14:24:51
/mnt/e/degeneraatti/node_modules/random-reddit/dist/functions.js:15
    utils_1.logger.trace('Response', child.data);
                                           ^

TypeError: Cannot read property 'data' of undefined
    at Object.getPost (/mnt/e/degeneraatti/node_modules/random-reddit/dist/functions.js:15:44)
    at processTicksAndRejections (node:internal/process/task_queues:94:5)
    at async getImage (/mnt/e/degeneraatti/node_modules/random-reddit/dist/functions.js:27:16)
    at async RedditService.getRedditImage (/mnt/e/degeneraatti/dist/reddit/reddit.service.js:19:23)
    at async /mnt/e/degeneraatti/dist/app.controller.js:35:31

the code I'm running

//reddit.service.ts
  async getRedditImage(fromSubReddit: string) {
    try {
      const image = await getImage(fromSubReddit);
      return image;
    } catch (error) {
      return error;
    }
  }

then I call it

//app.controller.ts
      if (messageMatch && messageMatch.commandPrefix === '!r') {
        const image = await this.redditService.getRedditImage(messageMatch.command).catch((error) => {
          console.log('error: ', error)
        })

        const message = `result ${messageMatch.command} :: ${image ? image : 'ERROR'} `
        console.log(message)

        this.appService.replyToChannel(this.defaultChannel, message);
      }

and reply to channel

//app.service.ts
  public replyToChannel(channelName: string, message: string) {
    const channel = this.channelMap.get(channelName);
    channel.say(message);
  }

I managed to stop it crashing by catching the errors for now.

TheCuriousGoose commented 3 years ago

Still having the same problem as @ex1tium, running practically the same code as before. also is there a way to disable the HTTP Get console message.

Thanks in advance

Tormidal commented 3 years ago

Hi;

Sorry for the multiple comments - git isn't cooperating with my brain. I'm also having the same undefined issue on 2.0.2-0. My code is not quite as refined; but its work so far without much issue. r/ecchi is the problematic subreddit in this case, but i've seen it on others as well.

async function getpic(fromSubreddit) {
    console.log('received ${fromSubreddit}');
    try {
        const image = await getImage(fromSubreddit);
        console.log('retrieved pic at ${image}');
        return image;
    } catch (error) {
        console.log(error);
        return error;
    }
}

is called by

(async() => {
            dataurl = await getpic(categories[command][0]);
                message.reply(dataurl);
        })();

And lastly the error:

TypeError: Cannot read property 'data' of undefined
    at Object.getPost (D:\Desktop\discord bot\node_modules\random-reddit\dist\functions.js:15:44)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async getImage (D:\Desktop\discord bot\node_modules\random-reddit\dist\functions.js:27:16)
    at async getpic (D:\Desktop\discord bot\bot2.js:49:17)
    at async D:\Desktop\discord bot\bot2.js:168:15
AlexAtHome commented 3 years ago

@Tormidal, @ex1tium, @TheCuriousGoose, make sure you pass ecchi (or ['ecchi']) in the argument, not r/ecchi

The following code works correctly:

async function test() {
  const image = await getImage('ecchi').catch(e => {
    console.log(e);
    return;
  })

  console.log(image);
}

test()

image

Tormidal commented 3 years ago

@mamoruuu

Hi,

I have copied your code exactly and triggered the function.

image

I've also removed package and made sure it was installed as 2.0.2-0 to confirm that it is not on an incorrect version. I've done the same with random-reddit's dependencies.

I'm at a loss at what else I can do on my end to try to help troubleshoot.

AlexAtHome commented 3 years ago

Well, I'm lost too :( this exact snippet of code work on my end.

But I use Linux. I have a theory that node-fetch might work differently on Windows. I'm gonna check it tomorrow

Tormidal commented 3 years ago

Sounds good! I'll keep my eye on it and test it when its available :)

In the meantime, I'll see if I can get a Linux platform to run this off of.

Tormidal commented 3 years ago

@mamoruuu For what its worth; I spun up a Ubuntu 20.04 VM and still getting the same issue.

image

Here is my current modules on the VM as well:

image

If there is anything else I can provide that can help, let me know :)

AlexAtHome commented 3 years ago

It clearly says that the HTTP request was successful 🤔

So it's definitely the problem with handling the response (and not with the OS, how could I think about it?)

Sorry for delay, I'm still busy, I'll get to this problem asap

Tormidal commented 3 years ago

Happy to help, I've found a number of subreddits affected by the issue: Not sure if having multiple examples will help, but I figured it was worth noting in case there's similarities.

Subreddits ``` HentaiBeast, ecchi, grailwhores, AnimeFeet, HypnoHentai, thighdeology, adorablehentai ```
AlexAtHome commented 3 years ago

My guess is you catch the Cannot read property "data" of undefined errors because the request that is sent to https://reddit.com/r/whatever_sub_you_want/random.json?limit=1 returns the following error:

{
  "message": "Too Many Requests",
  "error": 429
}

You can get such response if you run curl --location https://reddit.com/r/whatever_sub_you_want/random.json?limit=1

When I was testing different requests with curl to the same location the custom header User-Agent works for me. I'll also added some more headers just in case.

AlexAtHome commented 3 years ago

I just released the version 2.0.2-1, please, check if it solves your problem.

If you get errors, please re-run the same script with an environment variable CONSOLA_LEVEL=4. Like this

CONSOLA_LEVEL=4 npm run start

This will output the actual response from the request in the console

TheCuriousGoose commented 3 years ago

Sorry for the delayed reaction, but now I only get 1 image and not a random one from the subreddit, I did noticed the image is the currently hot on the subreddit

Tormidal commented 3 years ago

Apologies, been a busy week as well -

As @TheCuriousGoose said, I am getting the same image repeatedly when requesting from a subreddit affected by the issue.

AlexAtHome commented 3 years ago

Sorry for the delayed reaction, but now I only get 1 image and not a random one from the subreddit, I did noticed the image is the currently hot on the subreddit

I think it's because they're not just hot on the subreddit - they're pinned by moderators.

Also further testing shows that putting /random.json?limit=1 doesn't seem to work - it just returns the first post from hot posts. So I just made getPost to just get the list of posts and pick a random one myself.

Try 2.0.2-2 now, it should work fine now.

AlexAtHome commented 3 years ago

@TheCuriousGoose did the last version work for you?

TheCuriousGoose commented 3 years ago

@TheCuriousGoose did the last version work for you?

Yes it did, everything is fixed now

TheCuriousGoose commented 3 years ago

@TheCuriousGoose did the last version work for you?

Yes it did, everything is fixed now

AlexAtHome commented 3 years ago

Okay, I will release the proper version 2.0.2 with this fix now