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

Simple fetch JSON data in ReactJS

So I got this JSON response:

{
    "message": "OK",
    "data": [
        {
            "id": 5,
            "full_name": "John Donovan",
            "username": "j.donovan"
        }
    ]
}

I need to get the "full_name" and "username" values.

What I have done is this (I got a "message" value):

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

axios.post('http://localhost/user/login', sendData)
        .then((result) => {         
            if (result.data.message === 'OK') {

                console.log(result.data.username});
            }
        })

I successfully get the value of "message" but failed to get the value for "full_name" and "username". The result for console.log above is "undefined".

Tried googling and searching in this forum still have not found the answer. Please your correction.

Thank you.

>Solution :

data is an array. need to access the index

console.log(result.data.data[0].username})
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