Next.js Discord

Discord Forum

CORS error

Unanswered
American Chinchilla posted this in #help-forum
Open in Discord
American ChinchillaOP
so basically i have this two functions
export async function getGuilds(
  authorization: string
): Promise<PartialGuild[]> {
  const res = await fetch("https://discord.com/api/users/@me/guilds", {
    headers: {
      authorization,
    },
  })
  if (!res.ok) {
    throw new Error(`Failed to fetch guilds (Code: ${res.status})`)
  }
  return await res.json()
}

export async function getChannels(guildId: string): Promise<any> {
  const res = await fetch(
    `https://discord.com/api/guilds/${guildId}/channels`,
    {
      headers: {
        authorization: `Bot ${process.env.TOKEN}`,
      },
    }
  )
  if (!res.ok) {
    throw new Error(`Failed to fetch channels (Code: ${res.status})`)
  }

  return await res.json()
}
and while the first one works the second says this
Access to fetch at 'https://discord.com/api/guilds/1061263631511191632/channels' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
why is that

1 Reply

American ChinchillaOP
import { useCurrentGuild } from "@/app/contexts/CurrentGuildContext"
import { getChannels } from "@/lib/utils"

export default async function ChannelSelect() {
  const guild = useCurrentGuild()
  const channels = await getChannels(guild.id)
  return (
    <div>
      <p>ChannelSelect</p>
    </div>
  )
}
component i use that function at ^