Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

214
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda