The problem I am getting is I need to get the ID from a API response. The information is in a weird format I have not worked with before so I don’t know how I would get the ID data from this response.
(This is my first question so I am sorry if it is not very clear, feel free to leave comments on how I can fix it)
I am interacting with a API and the response data is:
{
data: [
{
requestedUsername: 'username',
hasVerifiedBadge: false,
id: 7838473284,
name: 'username',
displayName: 'username'
}
]
}
I tried using .data
but then I would get this result:
[
{
requestedUsername: 'username',
hasVerifiedBadge: false,
name: 'username',
displayName: 'username'
}
I am trying to get the data for id though and can’t find a way to get it
My current code:
const userid = await axios({
method: 'post',
url: 'https://users.roblox.com/v1/usernames/users',
data: {
"usernames": [
username
],
"excludeBannedUsers": true
},
})
console.log(userid.data.id)
2
Answers
The
data
is an array, so it would be:The data field in the response is an array, and you need to access the first element of this array to get information about the user. Since you want the id field of the user, you can do this:
In this example, I’ve separated the steps to make it clearer, but you can also access the id field directly like this: