Rian
Rian

Reputation: 57

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):

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.

Upvotes: 0

Views: 37

Answers (1)

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41387

data is an array. need to access the index

console.log(result.data.data[0].username})

Upvotes: 1

Related Questions