Home > database >  Heroku FetchError: invalid json response body at https://some-random-api.ml/meme reason: Unexpected
Heroku FetchError: invalid json response body at https://some-random-api.ml/meme reason: Unexpected

Time:06-03

I tried to add a function to generate random meme to my Discord bot running on Heroku, but every time I try running it, it just gives me this error: FetchError: invalid json response body at https://some-random-api.ml/meme reason: Unexpected token < in JSON at position 0. I'm wondering if Heroku has something similar to this? If not, is there a problem with my code (posted below):

const memes = require("random-memes");
const { randomHexColor } = require('../admin/colorgen.js');


module.exports = {
    name: 'meme',
    description: 'Never mind what the description is!',
    async execute(message, args, Discord, Client, bot) {
        memes.random().then(meme => {
            
            const newEmbed = new Discord.MessageEmbed()
            .setColor(randomHexColor())
            .setTitle(meme.caption)
            // .setURL(meme.image)
            .setDescription(`category: ${meme.category}`)
            .setImage(meme.image);
            
            message.channel.send({ embeds: [newEmbed] });
        })
    }
}

CodePudding user response:

The API may have just been down at the time, I have run into the same error before. Try using a .catch after the .then(... => { ... }) to handle the error and possibly debug.

memes.random().then(meme => {
    console.log(meme)
}).catch(err => {
    console.log(err)
});

If you are still getting an error with the NPM package, try just sending a node-fetch request to https://some-random-api.ml/meme which is the API that random-memes uses (source code).

const fetch = require('node-fetch');

const response = await fetch('https://some-random-api.ml/meme');
const data = await response.json();

console.log(data);

data should return something like

{
  "id": number,
  "image": string,
  "caption": string,
  "category": string
}

Then you can go from there.

  • Related