0

I am requesting some basic info from the back end using axios but for some reason unable to render the data on screen. Below is my basic App component using hooks and a map function to return a surname

import React, { useState, useEffect } from 'react';
import FullCalendar from '@fullcalendar/react'
import dayGridPlugin from '@fullcalendar/daygrid'
import interactionPlugin from '@fullcalendar/interaction';
import { Router } from '@reach/router'
import 'bootstrap/dist/css/bootstrap.css'
import axios from 'axios'

import './custom.css'

const App = () => {

    const [patients, setPatient] = useState([])

    useEffect(() => {
        axios.get('http://localhost:5000/api/patient').then(response => {
            console.log(response.data)
            setPatient(response.data)
        })
    }, [])



    return (

        <>
            <div>
                <ul>
                    {patients.map(p => (
                        <li>{p.surname}</li>
                    ))}
                </ul>

            </div>

        </>

    )


}







export default App

When I check the dev tools I am bringing back all of data

enter image description here

I cannot see how my map function 'is not a function' can anyone help me out here please I get the below error message which is annoying

enter image description here

4
  • please add console.log(response.data) screenshot. Commented Dec 9, 2020 at 15:19
  • try add ? to your {patients?.map(p => (<li>{p.surname}</li>))} to see whether there's data or not captured on render. If nothing happen, then that means your patients array is empty Commented Dec 9, 2020 at 15:19
  • Hey lala sorry, I do get all the data back in the array when i put the {patients?.map} in the function Commented Dec 9, 2020 at 15:23
  • Please add a screenshot of console.log(response.data) Commented Dec 9, 2020 at 15:24

3 Answers 3

1

Try to use Async and Await for API call.

useEffect(function() {
   async function fetchPatients() {
      const response = await 
                       fetch('http://localhost:5000/api/patient');
      const json = await response.json();
      setPatient(json.data);
    }
   fetchPatients();
 }, []);
Sign up to request clarification or add additional context in comments.

1 Comment

I think this could be an axios issue Abu Sufain, your solution solved the issue. But I want to use axios and not fetch :/
1

try this fixes:-

import React, { useState, useEffect } from 'react';
import FullCalendar from '@fullcalendar/react'
import dayGridPlugin from '@fullcalendar/daygrid'
import interactionPlugin from '@fullcalendar/interaction';
import { Router } from '@reach/router'
import 'bootstrap/dist/css/bootstrap.css'
import axios from 'axios'

import './custom.css'

const App = () => {

  const [patients, setPatient] = useState([])

  useEffect(() => {
    (async () => {
      try {
        // fetching all patirnts
        let res = await axios.get("http://localhost:5000/api/patient");
        setPatient(res.data);
      } catch (err) {
        console.log(err);
      }
    })();
  }, []);

  return (
    <>
      <div>
        <ul>
          {patients?.map(p => (
            <li>{p.surname}</li>
          ))}
        </ul>
      </div>
    </>
  )
}

export default App

6 Comments

None of the axios solutions have worked so far :/
i just edit my code. try the above updated code
Still no joy :( It must be an issue with axios as ive never had this issue before
@Rowandinho try using fetch instead of axios then.
just edit it again. Give it a try. If no go again, then for now, just go with fetch
|
0

A working code, for anyone who stupidly wastes too much time on this problem in the future. The data was nested meaning I had to setPatient to response.data.data. I was then able to pull all required info using Axios

   useEffect(() => {
        axios.get('http://localhost:5000/api/patient').then(response => {
            
            setPatient(response.data.data)
        })
    }, [])

Comments

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.