[英]Why is my array variable undefined in my component when I try to use array methods on it?
I'm having an issue with displaying an array of Pokemon moves that I've retrieved from the database (using a rails/react/redux).我在显示从数据库中检索到的一系列口袋妖怪动作时遇到问题(使用 rails/react/redux)。 The strange thing is that when I display the entire array without trying to use
join
on it, everything's fine.奇怪的是,当我显示整个数组而不尝试使用
join
时,一切都很好。 But when I try to use join
on it, I get this error message: Uncaught TypeError: Cannot read property 'join' of undefined
.但是当我尝试对其使用
join
时,我收到此错误消息: Uncaught TypeError: Cannot read property 'join' of undefined
。 And when I display the entire array variable as is, without modifying anything, it does display.当我按原样显示整个数组变量时,不修改任何内容,它确实显示。
I've gone through my code and I've confirmed that the routing is fine and the right action creators are being dispatched.我已经检查了我的代码并且我已经确认路由是好的并且正在调度正确的动作创建者。 I've also gone through my reducers and I can confirm that they are using the right action constants to make decisions as to what the new state object is.
我还检查了我的减速器,我可以确认他们使用正确的动作常量来决定新的状态对象是什么。 The only thing that I can think of is that maybe the problem lies in one of these places below:
我唯一能想到的是,问题可能出在以下这些地方之一:
// the component that renders my Pokemon details onto the page // 将我的 Pokemon 详细信息呈现到页面上的组件
class PokemonDetail extends React.Component {
componentDidMount() {
this.props.requestSinglePokemon(this.props.match.params.pokemonId);
}
componentDidUpdate(prevProps) {
if (prevProps.match.params.pokemonId !== this.props.match.params.pokemonId) {
this.props.requestSinglePokemon(this.props.match.params.pokemonId);
}
}
render() {
const { pokemon } = this.props;
if (!pokemon) return null;
return (
<section className='pokemon-detail'>
<figure>
<img src={ pokemon.image_url } alt={ pokemon.name } />
</figure>
<ul>
<li><h1>{ pokemon.name }</h1></li>
<li>Type: { pokemon.poke_type }</li>
<li>Attack: { pokemon.attack }</li>
<li>Defense: { pokemon.defense }</li>
<li>Moves: { pokemon.moves.join(', ') }</li>
</ul>
</section>
);
}
}
// the reducer //减速器
const pokemonReducer = (state = {}, action) => {
Object.freeze(state);
let poke;
switch(action.type) {
case RECEIVE_ALL_POKEMON:
return Object.assign({}, state, action.pokemon);
case RECEIVE_SINGLE_POKEMON:
poke = action.payload.pokemon;
return Object.assign({}, state, { [poke.id]: poke });
default:
return state;
}
}
// the action creators //动作创建者
export const requestSinglePokemon = id => dispatch => {
dispatch(startLoadingSinglePokemon());
return APIUtil.fetchSinglePokemon(id).then(pokemon => dispatch(receiveSinglePokemon(pokemon)));
}
export const receiveSinglePokemon = payload => ({
type: RECEIVE_SINGLE_POKEMON,
payload
});
// the backend APIUtil method // 后端 APIUtil 方法
export const fetchSinglePokemon = id => (
$.ajax({
method: 'GET',
url: `api/pokemon/${id}`
})
);
It's the strangest thing...when I do { JSON.stringify(pokemon.moves) }
I can literally see that it is an array of strings (of Pokemon moves).这是最奇怪的事情......当我做
{ JSON.stringify(pokemon.moves) }
我可以从字面上看到它是一个字符串数组(口袋妖怪移动)。 And even when I just display it like this, { pokemon.moves }
, it seems to be correctly grabbing the right Pokemon;甚至当我像这样显示它时,
{ pokemon.moves }
,它似乎正确地抓住了正确的 Pokemon; the name, type, attack, and defense text is all there and the moves is also displayed (although not correctly formatted).名称、类型、攻击和防御文本都在那里,并且还显示了动作(尽管格式不正确)。
NOTE: Upon observing my log (it shows me the action creators that are dispatched when I interact with the page), the error with join
seems to pop up before (or instead?) of the action creator that should have been dispatched (ie I see that the START_LOADING_SINGLE_POKEMON
action is never dispatched before this error pops up)注意:在观察我的日志时(它向我显示了当我与页面交互时分派的动作创建者),
join
错误似乎在应该被分派的动作创建者之前(或相反?)弹出(即我看到START_LOADING_SINGLE_POKEMON
操作在此错误弹出之前从未被调度)
for the first render of your app , no actions have been dispatched.对于您的应用程序的第一次渲染,尚未分派任何操作。 componentDidMount occurs after initial render.
componentDidMount 发生在初始渲染之后。 hence, unless the parent component of "PokemonDetail" is passing an intial pokemon object with key "moves " whose value is of type array, it would casue this error for first render.
因此,除非“PokemonDetail”的父组件传递一个带有键“moves”的初始 pokemon 对象,其值为数组类型,否则它会在第一次渲染时导致此错误。
you can either have conditional rendering inside "PokemonDetail" component based on "Pokemon" object or have inital state of "pokemon" contain moves array in reducers.您可以基于“Pokemon”对象在“PokemonDetail”组件内进行条件渲染,或者让“pokemon”的初始状态包含reducers中的移动数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.