[英]React TypeError: Cannot read property 'length' of undefined
I building small react app.我正在构建小型反应应用程序。 Main task is creating app about pokemons with PokeApi.
主要任务是使用 PokeApi 创建关于口袋妖怪的应用程序。 When I clicking on pokeCard, on my sidebar must to display main information about pokemon.
当我点击 pokeCard 时,我的侧边栏必须显示有关 pokemon 的主要信息。 So, I want to be displayed number of pokemon.
所以,我想显示口袋妖怪的数量。 How we know, all numbers starts at #.
我们怎么知道,所有数字都从#开始。 But in PokeApi numbers of pokemons are not starts with # and doesn't containt any 0. For example, number of Charmander is #004, but in PokeApi Id is 4. So, I created condition with logical operators.
但是在 PokeApi 中,口袋妖怪的数量不是以 # 开头,也不包含任何 0。例如,Charmander 的数量是 #004,但在 PokeApi Id 中是 4。所以,我用逻辑运算符创建了条件。 Please, look:
敬请期待:
{(pokemon.id.length === 1 && <span>#00{pokemon.id}</span>) ||
(pokemon.id.length === 2 && <span>#0{pokemon.id}</span>) ||
(pokemon.id.length === 3 && <span>#{pokemon.id}</span>)}
I hope I created right.我希望我创造对了。 But, on my screen I see
TypeError: Cannot read property 'length' of undefined
.但是,在我的屏幕上,我看到
TypeError: Cannot read property 'length' of undefined
。 I think, it's because,before I will be clicking, pokemon.id is undefined.我想,这是因为,在我点击之前,pokemon.id 是未定义的。 I try to create
typeof(pokemon.id) == "undefined" && <span>loading</span>
, but it doesn't work.我尝试创建
typeof(pokemon.id) == "undefined" && <span>loading</span>
,但它不起作用。
It's code, how I set Pokemon.这是代码,我如何设置口袋妖怪。
const [SelectedPokemon, setSelectedPokemon] = useState([]);
const fetchPokemonDetails = (pokemonId) => {
fetch(`${API_URL}${pokemonId}`)
.then((result) => result.json())
.then((result) => {
setSelectedPokemon(result);
}, setLoadingForSelectedPokemon(false))
.catch((error) => console.log("Error:", error));
};
Can you help me fix it?你能帮我修一下吗?
You are trying to access the property before its defined.您正在尝试在定义之前访问该属性。 Can you try with the code snippet below?
你可以试试下面的代码片段吗?
{pokemon.id !== undefined ?
(pokemon.id.length === 1 && <span>#00{pokemon.id}</span>) ||
(pokemon.id.length === 2 && <span>#0{pokemon.id}</span>) ||
(pokemon.id.length === 3 && <span>#{pokemon.id}</span>)
: null}
You should ensure the data called properly.您应该确保正确调用数据。 it is occurred because your data is not ready yet.
这是因为您的数据还没有准备好。
the code should be like this代码应该是这样的
{(pokemon && pokemon.id.length === 1 && <span>#00{pokemon.id}
</span>) ||
(pokemon && pokemon.id.length === 2 && <span>#0{pokemon.id}
</span>) ||
( pokemon && pokemon.id.length === 3 && <span>#{pokemon.id}
</span>)}
The main problem is that you are trying to use pokemon.id
before it is actually defined.主要问题是您试图在
pokemon.id
实际定义之前使用它。 I would recommend putting your statements in another if like:如果像这样,我建议将您的陈述放在另一个中:
{ pokemon.id ? this.handleId(pokemon.id) : "" }
where在哪里
handleId = (id) => {
let holder = "#000";
let arr = holder.split("");
id.split("").reverse().map((e, i) => arr[3 - i] = e);
return arr.reduce((a, b) => a + b);
}
I believe this solves your problem and handleId
can be used with an id
of any length.我相信这可以解决您的问题,并且
handleId
可以与任何长度的id
一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.