当我从 fetch 调用中调用数据时,一个部分会抛出错误,但仍然正确登录控制台,而其他所有部分都工作正常

问题描述 投票:0回答:1

我正在使用 DND5E API 并做出反应。我调用数据并将其保存到一个名为

primaryChoices
的变量中,该变量是一个从类部分下 api 中的 `proficiency_choices 开始的数组 (https://www.dnd5eapi.co/api/classes/monk)。

我用

.map
迭代它,然后使用
element.from.options
(也是一个数组),并使用嵌套
.map
迭代它并提取
options.item.index
的值。

这个逻辑适用于除僧侣之外的所有职业。它的设置与 api 中的所有其他类相同,甚至可以使用 this

.map

函数之外正确记录
console.log(primaryChoices[0].from.options[0].item.index)

同一个控制台日志也记录其他类,我什至可以毫无错误地显示它们的任何信息。

// after the data is called from the api it gets saved to character.class.primary
const primaryChoices = character?.class?.primary?.proficiency_choices


                if(!isPrimaryEmpty && isSecondaryEmpty){

                return (primaryChoices.map((element) => (
                    <div>
                    <strong>Choose {`${element.choose}`} for {`${primary.name}`}</strong>
                       
                        {element.from.options.map((option) => {

                            return(
                                <p>
                                    <input onChange={(e) => handleCheck(e, element, option)} type='checkbox' name={option.item.index} />
                                    <label htmlFor={option.item.index}>{option.item.name}</label>
                                </p>
                            )
                        })}

                    </div>
                )))

            }else if(...){...}

element.choose
primary.name
都适用于任何课程

我检查了路径,它记录正确,没有抛出错误,我转到 api 页面并仔细检查了路径。

javascript reactjs api
1个回答
0
投票

whatsapp

dfgghgfbfgbfgghggfggbgbgbdgbghngfdgnhdnfgngnfn

© www.soinside.com 2019 - 2024. All rights reserved.