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

216
Views
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 answers
Answer question

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 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!