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

212
Views
Cómo mostrar el error de respuesta cuando existe pero se desconoce hasta que se recibe

Usando Redux Toolkit, tengo un asyncThunk que me gustaría mostrar errores. Cuando intento:

El thunk:

 // Save a new role export const saveRole = createAsyncThunk( "roles/saveRole", async (role, { rejectWithValue }) => { try { const response = await api.post("/api/roles/", role); return response.data; } catch (err) { return rejectWithValue(err.response.data); } } );

El reductor:

 .addCase(saveRole.rejected, (state, action) => { state.error = action.payload; state.isLoading = false; });

El div quiero mostrar un error:

 {roles.error && ( <div className="error-container"> {roles.error.employee_type} </div> )}

Lo que da este error:

TypeError no capturado: no se pueden leer las propiedades de nulo (leyendo 'employee_type')

Lo que quiero es poder identificar si el error se trata de un campo específico y, de ser así, apuntar a ese campo exacto.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Encontré esta extraña sintaxis para resolver el problema. Parece verificar si el elemento existe solo cuando no está indefinido:

 {roles.error?.employee_type && ( <div className="error-container"> {roles.error.employee_type} </div> )}
about 4 years ago · Juan Pablo Isaza 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!