Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
Hacer clic en siguiente/enlace provoca un problema en getInitialProps

Estoy tratando de agregar un enlace para redirigir a los usuarios a la página de creación, pero cuando hice clic en el enlace, recibí el siguiente error:

 TypeError: Cannot read properties of undefined (reading 'cookies')

Código de enlace:

 <Link href={link}>Add New Employee</Link>

getInitialProps // _app.tsx

 MyApp.getInitialProps = async (context: any) => { const {req, res, pathname} = context.ctx; const {language, auth_key} = req.cookies; return { lang: language ?? 'ar', token: auth_key ?? null, } };
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Debe escribir código isomorfo dentro MyApp.getInitialProps porque ese código puede ejecutarse tanto en el servidor (durante la carga de la primera página) como en el cliente (durante las navegaciones del lado del cliente).

De la documentación de getInitialProps :

Para la carga de la página inicial, getInitialProps se ejecutará solo en el servidor. getInitialProps luego se ejecutará en el cliente cuando navegue a una ruta diferente a través del componente next/link o usando next/router . Sin embargo, si se usa getInitialProps en un _app.js personalizado, y la página a la que se navega implementa getServerSideProps , getInitialProps se ejecutará en el servidor.


Aquí hay una posible implementación para evitar su error. Comprueba si req está definido antes de acceder a req.cookies . Si es así, las cookies se recuperan del document en su lugar.

 // Get cookie from `document` on the client-side const getCookie = (name) => { const match = document.cookie?.match(new RegExp(`(^| )${name}=([^;]+)`)); return match?.[2]; }; MyApp.getInitialProps = async (context) => { const { req } = context.ctx; const language = req ? req.cookies.language : getCookie('language'); const auth_key = req ? req.cookies.auth_key : getCookie('auth_key'); return { lang: language ?? 'ar', token: auth_key ?? null, } };
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!