I have a Discord bot that makes some API calls. When I run the bot from my computer, it all works fine, however when I push my code to Heroku (where I am hosting my bot) the API call returns this error:
UnhandledPromiseRejectionWarning: FetchError: invalid json response body at https://sky.shiiyu.moe/api/v2/profile/SirArchibald97 reason: Unexpected token < in JSON at position 0
2021-01-07T11:45:02.931529+00:00 app[worker.1]: at /app/node_modules/node-fetch/lib/index.js:272:32
2021-01-07T11:45:02.931532+00:00 app[worker.1]: at processTicksAndRejections (internal/process/task_queues.js:97:5)
2021-01-07T11:45:02.931532+00:00 app[worker.1]: at async Object.execute (/app/methods/link.js:93:20)
2021-01-07T11:45:02.931532+00:00 app[worker.1]: at async Client.<anonymous> (/app/index.js:47:28)
2021-01-07T11:45:02.931832+00:00 app[worker.1]: (node:4) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
2021-01-07T11:45:02.931970+00:00 app[worker.1]: (node:4) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Edit: I am requesting two different APIs in my code, using the node-fetch
npm module. This is the one which returns the error:
let res = await fetch(`https://sky.shiiyu.moe/api/v2/profile/${player.displayname}`);
let data = await res.json();
And this is the one which works fine:
let p_response = await fetch(`https://api.hypixel.net/player?key=${api_key}&name=${args[1]}`);
let { player } = await p_response.json();
Edit 2: I have switched from node-fetch
to axios
in the hope that it might work, and I am now recieving a 503 status error instead. Once again, it works fine on my machine, but returns the error when I deploy to Heroku. Here is the new code:
const axios = require("axios");
let res = await axios.get(`https://sky.shiiyu.moe/api/v2/profile/${player.displayname}`);
let data = res.data;