Next.js Discord

Discord Forum

Application error: a client-side exception has occurred

Answered
Shaurya posted this in #help-forum
Open in Discord
code: https://sourceb.in/WgO37d1HFH
It works perfect in development, but when I put it to production and visited the page, the page said:
Application error: a client-side exception has occurred (see the browser console for more information).
what did I do wrong?
and I don't understand the error either
Answered by Shaurya
I was missing the env BOT_ID :please_cat:
View full answer

20 Replies

anyone?
show your code
I've provided the sourcebin link
useCommands?
"use client"

import { useEffect, useState } from "react"
import { ResponseData } from "../types/response"

export default function useCommands(): ApplicationCommand[] | null {
  const [commandsData, setCommandsData] = useState<ApplicationCommand[] | null>(null)

  useEffect(() => {
    async function fetchCommands() {
      const res = await fetch("/api/getCommands")
      const data = await res.json() as ResponseData<{ commands: ApplicationCommand[] | null }>
      setCommandsData(data.data.commands)
    }

    fetchCommands()
  }, [])

  return commandsData
}
it works on dev server
show the api route then :D
@Shaurya it works on dev server
everything works on dev dont worry thats not something new :sunglasses_1:
import { Response as _Response } from "@/lib/utils/response"
import { BotAuthorization } from "@/config/authorization"
import config from "@/config/config"

const appId = process.env.BOT_ID
const { dapi } = config

export async function GET() {
  try {
    const res = await fetch(`${dapi}/applications/${appId}/commands`, {
      headers: { "Authorization": BotAuthorization }
    })
    const data = await res.json()
    return Response.json(_Response(true, "Application commands fetched", { commands: data }))
  } catch (err) {
    return Response.json(_Response(false, "An error occured while fetching application commands", { commands: null }))
  }
}
ok so 1) do you need to fetch on the client?
can you do it in a server component instead?
2) can you find out whats causing the error specifically.. how you would do this is comment out stuff so first comment the api route and just put dummy data.. if it breaks then the api isnt the issue.. if it works then the api is the issue etc
try that out and tell me out of the 3

api route
helper
component

which is causing the error
I'd need to put it in prod multiple times though because I don't see anything wrong here and on the dev server it works all fine, some way I could just check it on the dev server?
npm run build on your pc
thats just the build command
so you dont need to keep pushing to prod
to start its npm run start
after build btw
I was missing the env BOT_ID :please_cat:
Answer