Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why not showing the return value of the function?

function
in console.log I can see the correct results

     const getName = (id) => {

        var name = "";
        axios.get( `http://localhost:3001/students/names/${id}`).then((response) => {

          name = response.data[0].Fname + " " + response.data[0].Lname;
          console.log(name);
        })
        .catch((err) => {
          console.log(err);
        });

        return name;
        
      };

render method

 {results.map((value,key)=>(

                                      <tr key={key}>

                                          <td className='columnData'>
                                              { (getName(value.Student_ID))  }
                                          </td> 


Why not showing the return value of the function ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

this is a async function so if you want to get the name just make a state.

const [name, setName] = useState('')
const getName = (id) => {
        axios.get( `http://localhost:3001/students/names/${id}`).then((response) => {

          setName(response.data[0].Fname + " " + response.data[0].Lname);
          console.log(name);
        })
        .catch((err) => {
          console.log(err);
        });

        return name;
        
      };

then you can use the name where ever you want

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading