getting error POST http://localhost:5000/users/signup 400 (Bad Request)

Viewed 39

I am creating an authentication system with redux but getting POST http://localhost:5000/users/signup 400 (Bad Request) while requesting to backend for login but this link is working to login with help of postman and also getting axios error AxiosError {message: 'Request failed with status code 400', name: 'AxiosError', code: 'ERR_BAD_REQUEST', config: {…}, request: XMLHttpRequest, …}

This is the code of authSlice.js

import * as api from "../api"
export const login=createAsyncThunk("auth/login",async({formValue,navigate,toast})=>{
    try{
        const response=await api.signIn(formValue);
        toast.success("success");
        navigate("/");
        return response.data;
    }catch(err){
        console.log(err)
    }
})

const authSlice=createSlice({
    name:"auth",
    initialState:{
        user:null,
        error:"",
        loading:false,
    },
    reducers:{

    },
    extraReducers:{
        [login.pending]:(state,action)=>{
            state.loading=true;
        },
        [login.fulfilled]:(state,action)=>{
            state.loading=false;
            localStorage.setItem("profile",JSON.stringify(...action.payload));
            state.user=action.payload;
        },
        [login.rejected]:(state,action)=>{
            state.loading=false;
            state.error=action.payload.message;
        },
    }
});
export default authSlice.reducer;

This is the code of api.js


const API =axios.create({baseURL:"http://localhost:5000"});
export const signIn=(formData)=>API.post("/users/signup",formData);

error image

1 Answers

Just a reminder that Redux will invoke through all your reducers in the initial state once you start the app or the client open your site. That means you would run code in your reducer with variables state being in the initial state.

I have seen your attached image and thought your issue should be here.

Related