So I'm new to React & I created a Registration page.
The problem is if I give an invalid input, then MongoDB throws an Error. It seems to work well in pages like Login
and UserProfile
, and not in UserRegistration
If there is a blank field, the page redirects to the homepage showing no error. Status code is given in console, but the error is not being thrown.
The state is supposed to update itself to have key-value pair of error but it updates to
userInfo: 'error message'
Reducer:
export const userRegisterReducer = (state = {}, action) => { switch (action.type) { case USER_REGISTER_REQUEST: return { loading: true } case USER_REGISTER_SUCCESS: return { loading: false, userInfo: action.payload } case USER_REGISTER_FAIL: return { loading: false, error: action.payload } case USER_LOGOUT: return {} default: return state } }
Action Creator:
export const register = (name, email, password) => async (dispatch) => { try { dispatch({ type: USER_REGISTER_REQUEST, }) const config = { headers: { 'Content-Type': 'application/json', }, } const { data } = await axios.post( '/api/users', { name, email, password }, config ) dispatch({ type: USER_REGISTER_SUCCESS, payload: data, }) dispatch({ type: USER_LOGIN_SUCCESS, payload: data, }) localStorage.setItem('userInfo', JSON.stringify(data)) } catch (error) { dispatch({ type: USER_REGISTER_FAIL, payload: error.response && error.response.data.message ? error.response.data.message : error.message, }) } }
State:
userLogin: { loading: false, message(pin):"User validation failed: name: Path `name` is required., email: Path `email` is required., password: Path `password` is required." stack(pin):"ValidationError: User validation failed: name: Path `name` is required., email: Path `email` is required., password: Path `password` is required. at model.Document.invalidate (C:\Users\Fayaz B\Desktop\Fayaz\Dev\Portfolio\ecommerce\node_modules\mongoose\lib\document.js:2693:32) at C:\Users\Fayaz B\Desktop\Fayaz\Dev\Portfolio\ecommerce\node_modules\mongoose\lib\document.js:2513:17 at C:\Users\Fayaz B\Desktop\Fayaz\Dev\Portfolio\ecommerce\node_modules\mongoose\lib\schematype.js:1241:9 at processTicksAndRejections (internal/process/task_queues.js:75:11)" } userRegister: { loading: false, message(pin):"User validation failed: name: Path `name` is required., email: Path `email` is required., password: Path `password` is required." stack(pin):"ValidationError: User validation failed: name: Path `name` is required., email: Path `email` is required., password: Path `password` is required. at model.Document.invalidate (C:\Users\Fayaz B\Desktop\Fayaz\Dev\Portfolio\ecommerce\node_modules\mongoose\lib\document.js:2693:32) at C:\Users\Fayaz B\Desktop\Fayaz\Dev\Portfolio\ecommerce\node_modules\mongoose\lib\document.js:2513:17 at C:\Users\Fayaz B\Desktop\Fayaz\Dev\Portfolio\ecommerce\node_modules\mongoose\lib\schematype.js:1241:9 at processTicksAndRejections (internal/process/task_queues.js:75:11)" }
MongoDB register controller:
const registerUser = asyncHandler(async (req, res) => { const { name, email, password } = req.body const userExists = await User.findOne({ email }) if (userExists) { res.status(400) throw new Error('User already exists') } const user = await User.create({ name, email, password, }) if (user) { res.status(201).json({ _id: user._id, name: user.name, email: user.email, isAdmin: user.isAdmin, token: generateToken(user._id), }) } else { res.status(400) throw new Error('Invalid user data') } })
https://stackoverflow.com/questions/66858901/redux-state-fails-to-update-on-error-cases March 30, 2021 at 01:33AM
没有评论:
发表评论