este es mi codigo
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 }; } Tengo el mismo código en otra 'página' y funciona bien. Este es absolutamente el mismo código con diferentes nombres y no funciona. Cuando intento iniciar sesión en ctx , devuelve indefinido. Además, esto es '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 }; } Entonces, enlazo de index.js a [games].js dinámicamente. No entiendo por qué ctx devolvió undefined :(
lo he resuelto Esto es Loco. Estuve atascado durante 2 días y la razón fue que en este código:
<Link href={`/fav-games/[games]`} as={`/fav-games/${game.id}`}> [games] es lo que necesito. Así que aquí en lugar de esto
const response = await fetch(`http://localhost:6969/favGames/${ctx.query.***id***}`);debería ser esto:
const response = await fetch(`http://localhost:6969/favGames/${ctx.query.***games***}`);