Javascript 错误类型错误:无法将未定义或null转换为对象
axios有这个错误,json加载很好,但是渲染有问题 actions.js:Javascript 错误类型错误:无法将未定义或null转换为对象,javascript,reactjs,redux,dispatch,axios,Javascript,Reactjs,Redux,Dispatch,Axios,axios有这个错误,json加载很好,但是渲染有问题 actions.js: export const getUser = (callback)=>{ return function(dispatch){ dispatch({type: 'FETCH_GET_USER_REQUEST'}); axios.get('https://jsonplaceholder.typicode.com/users') .then((respons
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)
}
})
}
}
reduceUser.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;
}
}
container.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)
}
查看您的控制台输出,当点击
FETCH\u GET\u USER\u成功时,您的问题最有可能出现在减速机中
您将返回以下内容:[…action.payload.data]代码>。尝试记录负载,负载上可能没有数据对象,因此会出现将未定义或null转换为对象错误。我打赌您只需要返回:[…action.payload]代码>从错误堆栈中,您可以看到错误是从代码中的getUserReducer
调用的,然后在\u toConsumableArray
中调用的,这是babel在将spread操作符传输到es5时创建的辅助方法
像@ಠ_ಠ 所暗示的,您得到错误是因为action.payload.data
不是对象,在这种情况下,应用扩展运算符将失败。([…action.payload.data]
)可能返回[…action.payload]代码>而不是返回[…action.payload.data]代码>@ಠ_ಠ 是的,你有权利吗,谢谢
const store = createStore(
reducers,
applyMiddleware(thunk, logger())
);