OiO.lk Blog javascript axiosClient.post() dose not pass the resolved then callback
javascript

axiosClient.post() dose not pass the resolved then callback


axiosClient.put() an .post() dose not pass to resolve callback (then ).
I work with laravel and react ,
I want to save data with put an post and navigate to an othher page by navigate("members") route
here is details

my view component :

export default function UserForm() {
  const navigate = useNavigate();
  let {id} = useParams();
  const onSubmit = ev => {
    ev.preventDefault()
    //console.log(user)
    if (user.id) {
      axiosClient.put(`/users/${user.id}`, user).then((data) => {
          setNotification('User was successfully updated')
          **navigate('/members')**
        })
        .catch(err => {
          const response = err.response;
          if (response && response.status === 422) {
            setErrors(response.data.errors)
          }
        })
    } else {
        
      axiosClient.post(`/users`, user)
        .then((data) => {
          setNotification('User was successfully created')
          **navigate('/members')**
        })
        .catch(err => {
          const response = err.response;
          if (response && response.status === 422) {
            setErrors(response.data.errors)
          }
        })
    }
  }

and It can’t execute navigate to ‘/members’

this is my axiosClient :

import axios from "axios"
import { UseStateContext } from "./context/ContextProvider";
const axiosClient =axios.create({
    baseURL:`http://127.0.0.1:8000/api`
})
axiosClient.interceptors.request.use((conf)=>{
    const token = localStorage.getItem("ACCESS_TOKEN")
    conf.headers.Authorization=`Bearer ${token}`;
    return conf;
});
axiosClient.interceptors.response.use((resp)=>{
    return resp
},
(error)=>{
    const {resp}=error;
    if(resp.status === 401){
        localStorage.removeItem("ACCESS_TOKEN");
    } else if (response.status === 404) {
        //Show not found
      }
    throw error;
}
);

export default axiosClient;

I try to debugg and it’s run the axiosClient.post(/users, user) save data in database and jumpe to axiosClient and so on…. without return to resolve then callback

plizzz any help

thank you



You need to sign in to view this answers

Exit mobile version