diff --git a/Client/src/Features/Auth/authSlice.js b/Client/src/Features/Auth/authSlice.js index 8c88dfca7..825bea3d6 100644 --- a/Client/src/Features/Auth/authSlice.js +++ b/Client/src/Features/Auth/authSlice.js @@ -18,7 +18,10 @@ export const register = createAsyncThunk( const res = await axios.post(`${BASE_URL}/auth/register`, form); return res.data; } catch (error) { - return thunkApi.rejectWithValue(error.response.data); + if (error.response.data) { + return thunkApi.rejectWithValue(error.response.data); + } + return thunkApi.rejectWithValue(error.message); } } ); @@ -28,7 +31,10 @@ export const login = createAsyncThunk("auth/login", async (form, thunkApi) => { const res = await axios.post(`${BASE_URL}/auth/login`, form); return res.data; } catch (error) { - return thunkApi.rejectWithValue(error.response.data); + if (error.response && error.response.data) { + return thunkApi.rejectWithValue(error.response.data); + } + return thunkApi.rejectWithValue(error.message); } }); @@ -43,8 +49,10 @@ const handleAuthFulfilled = (state, action) => { }; const handleAuthRejected = (state, action) => { state.isLoading = false; - state.success = action.payload.success; - state.msg = action.payload.msg; + state.success = false; + state.msg = action.payload + ? action.payload.msg + : "Failed to login or register"; }; const authSlice = createSlice({ diff --git a/Client/src/Features/Monitors/monitorsSlice.js b/Client/src/Features/Monitors/monitorsSlice.js index 64c35bc95..19d536797 100644 --- a/Client/src/Features/Monitors/monitorsSlice.js +++ b/Client/src/Features/Monitors/monitorsSlice.js @@ -18,7 +18,10 @@ export const getMonitors = createAsyncThunk( ); return res.data; } catch (error) { - return thunkApi.rejectWithValue(error.response.data); + if (error.response && error.response.data) { + return thunkApi.rejectWithValue(error.response.data); + } + return thunkApi.rejectWithValue(error.message); } } ); @@ -38,7 +41,10 @@ export const getMonitorsByUserId = createAsyncThunk( ); return res.data; } catch (error) { - return thunkApi.rejectWithValue(error.response.data); + if (error.response && error.response.data) { + return thunkApi.rejectWithValue(error.response.data); + } + return thunkApi.rejectWithValue(error.message); } } ); @@ -70,8 +76,10 @@ const monitorsSlice = createSlice({ }) .addCase(getMonitors.rejected, (state, action) => { state.isLoading = false; - state.success = action.payload.success; - state.msg = action.payload.msg; + state.success = false; + state.msg = action.payload + ? action.payload.msg + : "Getting montiors failed"; }) // ***************************************************** // Monitors by userId @@ -86,10 +94,11 @@ const monitorsSlice = createSlice({ state.monitors = action.payload.data; }) .addCase(getMonitorsByUserId.rejected, (state, action) => { - console.log(action); state.isLoading = false; - state.success = action.payload.success; - state.msg = action.payload.msg; + state.success = false; + state.msg = action.payload + ? action.payload.msg + : "Getting montiors failed"; }); }, });