JonathanLennartson
JonathanLennartson

Reputation: 13

TypeError: Cannot read properties of undefined (reading 'get') when trying to make an API-call with axios

I'm trying to get a list of orders from my backend API, but get this error in the console:

Uncaught TypeError: Cannot read properties of undefined (reading 'get')

It seems to me that the axios.get function dosen't work, and I don't know why... The same GET call works fine in a different part of my app.

I've tried the 0.24.00 and 0.20.00 version with no difference. Same error appears. I've also tried to put a questionmark after axios: axios?.get(.....). Here I get no error, but the API-call dosen't continue. It seems that axios is false or null..

Here's my code:

import { useEffect, useState } from "react";
import { axios } from "axios";

const Order = ({ customerId }) => {
  const [customerOrders, setCustomerOrders] = useState([]);

  const authConfig = {
    auth: {
      username: "username",
      password: "password",
    },
  };

  useEffect(() => {
    const getOrders = () => {
      axios
        .get(`http://localhost:8080/api/v1/myorders/${customerId}`, authConfig)
        .then((response) => {
          setCustomerOrders(...customerOrders, response.data);
          console.log(response.data);
        });
    };
    getOrders();
  }, []);

  return <p> {customerOrders.length} </p>;
};

Upvotes: 1

Views: 5635

Answers (1)

Antonio Pantano
Antonio Pantano

Reputation: 5042

I guess it should be

import axios from 'axios'

meaning it is exported as default and not as a named export.

Upvotes: 3

Related Questions