阵列的显示元素
我正在通过一个称为“ hometeam”的变量,当控制台记录时,它输出了
abbreviation: "Uls"
id: "08ew458ius6f"
name: "Ulster"
一个称为“ aveyteam”的变量,输出的数组相同,但是有了其他团队的数据,
我有一些代码可以找出我是否需要家乡或客车缩写;
const restartTeamAbbrevation = async (homeTeam, startTeamId, awayTeam) => {
if (startTeamId == {homeTeam.id} ) {
return homeTeam.abbreviation
} else {
return awayTeam.abbreviation
}}
然后,我希望这个值出现在我的前端,我正在尝试使用以下代码。
<Modal.Body>
<Row>
<Col>
<div className="d-flex">
<span className="py-1">Team: </span>
<span className="py-1">{ restartTeamAbbrevation() }</span>
</div>
</Col>
</Row>
</Modal.Body>
我有错误
Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'id')
I am passing through a variable called 'homeTeam' and when console logged it outputs
abbreviation: "Uls"
id: "08ew458ius6f"
name: "Ulster"
A variable called 'awayTeam' outputs the same array but with data of a different team
I have a bit of code to find out if I need the homeTeam or awayTeam abbreviation;
const restartTeamAbbrevation = async (homeTeam, startTeamId, awayTeam) => {
if (startTeamId == {homeTeam.id} ) {
return homeTeam.abbreviation
} else {
return awayTeam.abbreviation
}}
I then want this value to appear on my front end, i am attempting to use the following code;
<Modal.Body>
<Row>
<Col>
<div className="d-flex">
<span className="py-1">Team: </span>
<span className="py-1">{ restartTeamAbbrevation() }</span>
</div>
</Col>
</Row>
</Modal.Body>
I am getting the error
Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'id')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为当您调用该函数时,您不会传递诸如hometeam,startteamid,axewteam之类的参数,因此当函数称为这些值时,这些值等于不确定,并且当您尝试访问hometeam.id。 。
Because when you are calling that function, you are not passing the parameters like homeTeam, startTeamId, awayTeam, so when the function is called these values equals to undefined and when you try to access homeTeam.id inside the function, it gives you that error.
尝试将参数传递给您的RestartteamAbBreviation函数。
类似的事情:
您也有一些奇怪的条件,您可能想这样做:
还有一件事,RestartteamAbbreciation函数不需要是异步
Try passing arguments to your restartTeamAbbreviation function.
Something like this:
Also you have some strange condition, you might want to do this instead:
And one more thing, restartTeamAbbreciation function doesn't need to be async