【发布时间】:2021-11-13 04:56:26
【问题描述】:
我正在创建一个动态用户配置文件并尝试使用 React 映射一个对象。
我在下面有这段代码来获取用户数据:
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 [{…}, {…}, {…}, {…}, {…}]
*/
然后我使用array.find() 方法搜索特定用户:
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'}
那么我想要的是通过findTheUser对象进行映射并将数据显示给用户喜欢
{findTheUser.map(({{ name, email}}) => (
<div className="card">
<h1>{name}</h1>
<h2>{email}</h2>
</div>
))}
但它一直在抛出
TypeError: Cannot read properties of undefined (reading 'map')
【问题讨论】:
标签: reactjs firebase google-cloud-firestore mapping typeerror