I am creating a dynamic user profile and Am trying to map through an object using React.
I have this code below that fetches user data:
projectFirestore.collection("users").onSnapshot(snapshot => (
setUserData(snapshot.docs.map(doc => doc.data()))
))
}, [])
/*when the user data is fetched I console.log it
console.log(userData)
and it returns an array with objects like [{…}, {…}, {…}, {…}, {…}]
*/
then I search for a specific user using the array.find()
method:
const findTheUser = userData.find(function(data, index) {
if(data.email === 'minenhlecele@gmail.com')
return true;
});
/* I console.log it
console.log(findTheUser)
and it returns the matching object. the result looks like this */
//{name: 'Minenhle Cele', email: 'minenhlecele@gmail.com', photoUrl: 'https://lh3.googleusercontent.com/a-/AOh14Gj36jZuv0m2rxAN7Fx_78QWHbURpY-YeWb9g=s96-c'}
Then what I want is to map through the findTheUser object and display the data to the user like
{findTheUser.map(({{ name, email}}) => (
<div className="card">
<h1>{name}</h1>
<h2>{email}</h2>
</div>
))}
But it keeps throwing the
TypeError: Cannot read properties of undefined (reading 'map')
{findTheUser.name}
{findTheUser.email}