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

220
Vistas
How to display response error when it exists but is unknown until received

Using Redux Toolkit I have an asyncThunk which I would like to display errors from. When I try:

The 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);
    }
  }
);

The reducer:

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

The div I want to display an error:

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

Which gives this error:

Uncaught TypeError: Cannot read properties of null (reading 'employee_type')

What I want is to be able to identify if the error is about a specific field, and if so, target that exact field.

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

0

Found this weird syntax for resolving the issue. It seems to check if the item exists only when it is not undefined:

      {roles.error?.employee_type && (
        <div className="error-container">
          {roles.error.employee_type}
        </div>
      )}
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