【发布时间】:2023-03-20 20:23:01
【问题描述】:
当我尝试访问从数据库中提取的数据(receivedData 是一个对象)时,我收到 TypeError: Cannot read properties of undefined (reading '0') .我试过console.log我的receivedData,它确实有数据。这是我的social structure looks 的样子,它是我的receivedData object 的键之一。
const logoObj = {
"telegram": telegramWIcon,
"twitter": twitterWIcon,
"website": linkWIcon,
"whitepaper": fileWIcon
}
{receivedData.social[0] !== null &&
<div>
{ Object.entries(receivedData.social[0]).forEach((item) =>
item[1] !== null ? <SocialMedia url={logoObj.item[0]} link={item[1]} /> : ""
)}
</div>
}
【问题讨论】:
-
你确定你得到的错误不是因为
receivedData.social是undefined吗?尝试添加receivedData.social && receivedData.social[0] !== null ... -
@Alexandr 是的,这是error
-
哦!我明白了:您尝试获取对象
logoObj的字段item,但我没有看到对象中的字段。你的意思是logoObj[item[0]]?其余代码也提出了问题。起初,将forEach替换为map看起来是个好主意:第一个不返回任何内容。
标签: javascript reactjs