user7200977
user7200977

Reputation:

TypeError: Cannot read properties of undefined (reading 'status') in axios

This is the code from the freecodecamp tutorial (https://www.freecodecamp.org/news/how-to-build-react-based-code-editor/), the code is meant for react but my project is for next js and when I run it in the react project I don't get this error but when I run it in the next js project I get the error:

 TypeError: Cannot read properties of undefined (reading 'status')

The code where the error is occurring according to the error message.

axios
        .request(options)
        .then(function (response) {
            console.log("res.data", response.data);
            const token = response.data.token;
            checkStatus(token);
        })
        .catch((err) => {
            let error = err.response ? err.response.data : err;
            // get error status
            let status = err.response.status;
            console.log("status", status);
            if (status === 429) {
                console.log("too many requests", status);

                showErrorToast(
                    `Quota of 100 requests exceeded for the Day! Please read the blog on freeCodeCamp to learn how to setup your own RAPID API Judge0!`,
                    10000
                );
            }
            setProcessing(false);
            console.log("catch block...", error);
        });
};

Upvotes: 1

Views: 24391

Answers (1)

MauroFari
MauroFari

Reputation: 13

Take a look at Axious documentation. Looks like you are not even receiving a response, meaning error.response is left undefined. Maybe something wrong with the request options?

Upvotes: 0

Related Questions