Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

149
Vistas
Next.JS getInitialProps(ctx) ctx is undefined

This is my code

import NavLayout from "../../components/Navigation/Navigation";

export default function WorldOfWarcraft({ game }) {
    return (<NavLayout>
        {game.link}
    </NavLayout>)
}

WorldOfWarcraft.getInitialProps = async (ctx) => {
    const response = await fetch(`http://localhost:6969/favGames/${ctx.query.id}`);
    const game = await response.json();

    return { game };
}

I have same code in another 'page' and it works fine. This is absolutely same code with different names and it doesn't work. When I try to log ctx it returns undefined. Also, this is 'index.js'

import Link from "next/link";
import NavLayout from "../../components/Navigation/Navigation";

export default function FavGames({ games }) {
    console.log(games);
    return (<NavLayout title={'Fav Games Page'}>
        <h1>Favorite Games</h1>
        <ul>
            {games.map(game => (
                <li key={game.id}>
                    <Link href={`/fav-games/[games]`} as={`/fav-games/${game.id}`}>
                        <a>
                            {game.title}
                        </a>
                    </Link>
                </li>
            ))}
        </ul>
    </NavLayout>);
}

FavGames.getInitialProps = async () => {
    const response = await fetch('http://localhost:6969/favGames');
    const games = await response.json();

    return { games };
}

So, I link from index.js to [games].js dynamically. I don't understand why ctx returned undefined :(

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I have resolved it. This is crazy. I was stuck for 2 days and the reason was that in this code:

 <Link href={`/fav-games/[games]`} as={`/fav-games/${game.id}`}>

[games] is what I need. So here instead of this

const response = await fetch(`http://localhost:6969/favGames/${ctx.query.***id***}`);

it should be this:

const response = await fetch(`http://localhost:6969/favGames/${ctx.query.***games***}`);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda