阵列的显示元素

发布于 2025-02-11 04:03:17 字数 1017 浏览 3 评论 0原文

我正在通过一个称为“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放肆 2025-02-18 04:03:17

因为当您调用该函数时,您不会传递诸如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.

情泪▽动烟 2025-02-18 04:03:17

尝试将参数传递给您的RestartteamAbBreviation函数。
类似的事情:

        <Modal.Body>
        <Row>
            <Col>
                <div className="d-flex">
                    <span className="py-1">Team: </span>
                    <span className="py-1">{ restartTeamAbbreviation(homeTeam, startTeamId, awayTeam) }</span>
                </div>
            </Col>
        </Row>
        </Modal.Body>

您也有一些奇怪的条件,您可能想这样做:

if (startTeamId === homeTeam.id ) ...

还有一件事,RestartteamAbbreciation函数不需要是异步

Try passing arguments to your restartTeamAbbreviation function.
Something like this:

        <Modal.Body>
        <Row>
            <Col>
                <div className="d-flex">
                    <span className="py-1">Team: </span>
                    <span className="py-1">{ restartTeamAbbreviation(homeTeam, startTeamId, awayTeam) }</span>
                </div>
            </Col>
        </Row>
        </Modal.Body>

Also you have some strange condition, you might want to do this instead:

if (startTeamId === homeTeam.id ) ...

And one more thing, restartTeamAbbreciation function doesn't need to be async

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文