userSlice.js:22 Uncaught (in promise) TypeError: Cannot create property 'error' on string 'User created successfully!' at signInFailure (userSlice.js:22:19) at @reduxjs_toolkit.js?v=69e8a274:1773:26 #4723
Replies: 2 comments
-
|
The error This happens when a reducer replaces the entire state with the API response (a string) instead of updating individual properties. Most likely your // ❌ Wrong — reassigning the state parameter replaces the state with a string
signInSuccess: (state, action) => {
state = action.payload; // action.payload is "User created successfully!"
},In Redux Toolkit (which uses Immer), you must either mutate the draft state properties directly, or return a new state value. Reassigning the Fix: // ✅ Correct — mutate the draft state properties
signInSuccess: (state, action) => {
state.currentUser = action.payload; // action.payload should be the user object
state.loading = false;
state.error = null;
},
signInFailure: (state, action) => {
state.error = action.payload; // action.payload should be an error message string
state.loading = false;
},Also make sure your async call dispatches the user object to const data = await res.json();
dispatch(signInSuccess(data.user)); // pass the user object, not data.messageIf the API returns |
Beta Was this translation helpful? Give feedback.
-
|
The error In Redux Toolkit (Immer), if a case reducer returns a value, Immer replaces the entire state with that returned value. This is different from mutating the draft. // ❌ This causes the bug — returning replaces entire state
signInSuccess: (state, action) => {
return action.payload; // if payload is a string, whole state becomes that string
}
// ✅ Correct — mutate draft properties directly
signInSuccess: (state, action) => {
state.currentUser = action.payload;
state.loading = false;
state.error = null;
}Note: The specific string Fix both:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
#reducers for make requests to api in mern app
i try make the payload is string but it not solve the broblem !
those the backend api handling for signup and signing :
and this function that handle the signin request based on the actions from the userSlice :
Beta Was this translation helpful? Give feedback.
All reactions