Home > database >  Simple fetch JSON data in ReactJS
Simple fetch JSON data in ReactJS

Time:12-22

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.

CodePudding user response:

data is an array. need to access the index

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