0

Why username is logging undefined? i have a user object which is coming from another screen by navigation and route but i want to call the username from that user object so I tried to do console.log(user.username) but then it logs undefined and when I only log user then it gives whole user result as expected but then why is it giving username undefined?

const { user } = route.params;
console.log(user.username)
console.log(user)

User Log Result:

 LOG  [{"_id": "63c42922dc60a84421e8f843", "photos": [[Object]], "profileImg": "Image uri here",  "username": "Test"}]
wdwd
  • 1
  • 6
  • 4
    "*User Log Result:*" it shows an array which has one item in it. So, take the one item, instead of trying to get the username property of the array. Arrays do not have a username property. – VLAZ Jan 17 '23 at 08:09

1 Answers1

5

Examine the log closely. The result is an array, not an object.

To be able to access the username, you need to access the first element of the array.

console.log(user[0].username)

I suggest you try to figure out why params.user is an array and not an object.

kvetis
  • 6,682
  • 1
  • 28
  • 48