Next.js Discord

Discord Forum

api issue

Unanswered
Ruwbix posted this in #help-forum
Open in Discord
My code is functional, however my console is being (unnecessary?) spammed with 404 'errors'. I have an api route which checks if a player exists, if not it returns a res.status(400).send("Player not found"). This is expected, do I just change the status to 200?

1 Reply

Here's my code

// utils/api/player

/**
 * Retrieves a player by Steam ID.
 * @param {number} steamId - The server id.
 * @param {string} steamId - The Steam ID of the player.
 * @returns {Promise<Object>} A promise that resolves to the player object.
 */
export const getPlayerBySteamId = async (
  serverId: number,
  steamId: string
): Promise<Object> => {
  const response = await fetch(
    `${BASE_URL}${PLAYERS_API_ENDPOINT}getPlayerBySteamId?serverId=${serverId}&steamId=${steamId}`
  )

  if (response.status === 404) return null

  const data = await response.json()
  return data
}


// pages/api/players/getPlayerBySteamId

import { PrismaClient } from "@prisma/client"
import { NextApiRequest, NextApiResponse } from "next"

const prisma = new PrismaClient()

export default async function handle(
  req: NextApiRequest,
  res: NextApiResponse
) {
  try {
    const { serverId, steamId } = req.query

    // if server and steamid
    const player = await prisma.player_data.findFirst({
      where: {
        AND: [
          {
            steam_id: steamId as string,
          },
          {
            server_id: parseInt(serverId as string),
          },
        ],
      },
    })

    if (!player) return res.status(404).send("Player not found")

    return res.status(200).json(player)
  } catch (err) {
    return res.status(500).send("Internal Server Error")
  }
}