0

Hi Guys I have a Problem when i want to get Request from Axios through api than when i set selected data from axios in userDetail interface it gives error. please check sand box i have put all code and data format that returns from api

This is Code Sandbox all data of api and axios request is in it

1 Answer 1

1

Here are my changes Modified sandbox link

  1. userDetail(id: string) returns you a list of user info, no single user info. Advice: Name your methods with what they do. userDetail has no verb. So it could be getUserDetail, updateUserDetail, deleteUserDetail and so on.

//old code
userDetail: (id: string) =>
  axios.get<userDetail[]>(`/User/GetUserByID/${id}`),

//Correct
userDetail: (id: string) =>
    axios.get<userDetail>(`/User/GetUserByID/${id}`)
  1. Your state type was wrong. Advice: Name types, interfaces PascalCase Some typescript guidelines
/// your version array of any OR userDetail. I believe you want to store a single user detail
const [userDetails,setUserDetail]=useState<userDetail | []>([])


///correct
const [userDetails, setUserDetail] = useState<userDetail | undefined>()
  1. You had issues with loading. You were not waiting the proimse.
/// your version
  useEffect(()=>{
    // Agent File to access axios Request
    const result= agent.createUser.userDetail("1")
    setUserDetail(result);
  },[])

/// correct using Promise.then. Note that error is not handled

  useEffect(() => {
    // Agent File to access axios Request
    agent.createUser.userDetail("1").then((axiosResponse) => {
      setUserDetail(axiosResponse.data);
      setIsLoading(false);
    });
  }, []);

// correct using async await

useEffect(() => {
  //declare it
  const loadUserDetail = async () {
    const axiosResponse = await agent.createUser.userDetail("1");
    setUserDetail(axiosResponse.data);
    setIsLoading(false);
  }

  //call it
  loadUserDetail();
}, []);


Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot You Save My Time The Result is Fixed I will apply it on My Works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.