Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

594
Views
Axios.get() request returning undefined

In the below code Iam trying to use axios get request from await axios.get('/products/api') is only working i can see the data in the console but forawait axios.get('/users/api')Undefined is being returned on the console but when i try get request through Postman i can get the results .Iam stuck here.

''' const [userdata, setuserdata] = useState([]); const [products, setproducts] = useState([]);

useEffect(() => {
    const fetchUserData = async () => {
        
        const { users } = await axios.get('/users/api')
        setuserdata(users);

        const { data } = await axios.get('/products/api')
        setproducts(data);
    }
    fetchUserData();



}, []);
console.log(products)
console.log(userdata)
about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

Before you destructure your response console.log the response. I mean try:

const users = await axios.get('/users/api');
console.log(users)

It could be that users is not part of the default axios data object. If you still get undefined, then you need to double check that your api is returning the response (POSTMAN).

about 4 years ago · Santiago Gelvez Report

0

Update your code to this:

   const users = await axios.get('/users/api')
    setuserdata(users.data);

    const products = await axios.get('/products/api')
    setproducts(products.data);
about 4 years ago · Santiago Gelvez Report

0

Believe what you're trying to do is:

const [userdata, setUserData] = useState([])
const [products, setProducts] = useState([])

useEffect(() => {
    const fetchUserData = async () => {
        const users = await axios.get('/users/api')
        setUserData([...userdata, users])

        const data = await axios.get('/products/api')
        setProducts([...products, data])
    }
    fetchUserData()
}, [userdata, products])

console.log(products)
console.log(userdata)

If you just use [] on the useEffect it will only run once at the beginning but per memory React will throw a warning for products and userdata.

Think they'd also work better in their own useEffect. You could even make your code cleaner by creating a function and passing the path in to the function. Like:

const getData = async (path) => await axios.get(`/${path}/api`)
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!