0

I have an API, with Node and MongoDB.

I do the following component with this request :

export default function Balance() {
    const uid = useContext(UidContext)

    useEffect(() => {
        axios({
            method: "get",
            url: `${process.env.REACT_APP_API_URL}api/balance/${uid}`
        }).then(response => console.log(response.data[0]));
    }, [uid]);

    return (
        <section>
            Walet
        </section>
    )
}

And I get an error, AND the results I want : Result

I don't understand why I get an error, and then the code 200 ?

Thank you !

1 Answer 1

1

It is because your uid is null at first. You can do a null check:

  useEffect(() => {
      if(uid !== null){
        axios({
            method: "get",
            url: `${process.env.REACT_APP_API_URL}api/balance/${uid}`
        }).then(response => console.log(response.data[0]));
       }
    }, [uid]);

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

1 Comment

Why didn't I think of that ?! Thank you !

Your Answer

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