gpt4 book ai didi

javascript - 错误类型错误 : Cannot convert undefined or null to object

转载 作者:行者123 更新时间:2023-11-30 15:38:28 27 4
gpt4 key购买 nike

我在使用 axios 时遇到了这个错误,json 加载正常,但是渲染有问题

enter image description here

Action .js:

export const getUser = (callback)=>{
return function(dispatch){
dispatch({type: 'FETCH_GET_USER_REQUEST'});
axios.get('https://jsonplaceholder.typicode.com/users')
.then((response)=>{
dispatch({type:'FETCH_GET_USER_SUCCES', payload:response.data});
if (typeof callback === 'function') {
callback(null, response.data)
}
})
}
}

reducerUser.js

export const getUserReducer = (state=[], action) =>{
switch(action.type){
case 'FETCH_GET_USER_REQUEST':
return state;
case 'FETCH_GET_USER_FAILURE':
return state;
case 'FETCH_GET_USER_SUCCES':
return [...action.payload.data];
default:
return state;
}
}

容器.jsx

class GetUserContainer extends Component{
componentDidMount(){
this.props.getUser();
}
render(){
return(
<GetUserComponent allUser={this.props.allUser} />
)
}
}
function mapStateToProps(store){
return{
allUser:store.allUser
}
}
function matchDispatchToProps(dispatch){
return bindActionCreators({
getUser:getUser
}, dispatch)
}

商店.js

 const store = createStore(
reducers,
applyMiddleware(thunk, logger())
);

最佳答案

查看您的控制台输出,当 FETCH_GET_USER_SUCCES 操作被命中时,您的问题很可能出在您的 reducer 中。

您返回的是:[...action.payload.data];。尝试记录您的负载,负载上可能没有数据对象,因此将 undefined 或 null 转换为对象错误。我打赌你只需要返回:[...action.payload];

关于javascript - 错误类型错误 : Cannot convert undefined or null to object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41193273/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com