Javascript 未处理的拒绝(TypeError):无法读取属性';类型';未定义的。如何解决这个问题
我不明白为什么会出现这个错误。我正在尝试创建配置文件功能,该功能将使用axios获取数据,并通过用户ID显示数据。我尝试了很多东西,我对其他组件也做了同样的事情,一切都正常 ProfileContainer.jsJavascript 未处理的拒绝(TypeError):无法读取属性';类型';未定义的。如何解决这个问题,javascript,reactjs,Javascript,Reactjs,我不明白为什么会出现这个错误。我正在尝试创建配置文件功能,该功能将使用axios获取数据,并通过用户ID显示数据。我尝试了很多东西,我对其他组件也做了同样的事情,一切都正常 ProfileContainer.js import React from 'react'; import {connect} from 'react-redux'; import Profile from './Profile'; import setUserProfile from '../../redux/profil
import React from 'react';
import {connect} from 'react-redux';
import Profile from './Profile';
import setUserProfile from '../../redux/profile-reducer'
import * as axios from 'axios';
class ProfileContainer extends React.Component {
componentDidMount() {
axios.get(`https://social-network.samuraijs.com/api/1.0/profile/2`).then(response => {
this.props.setUserProfile(response.data);
});
};
render() {
return (
<Profile {...this.props} profile={this.props.profile} />
)
}
}
let mapStateToProps = (state) => ({
profile: state.profilePage.profile
});
export default connect(mapStateToProps,{setUserProfile})(ProfileContainer);
const ADD_POST = 'ADD-POST'
const UPDATE_NEW_POST_TEXT = 'UPDATE-NEW-POST-TEXT'
const SET_USER_PROFILE = 'SET_USER_PROFILE';
let initialState = {
posts: [
{ id: 1, message: 'How are you bro?)', likesCount: 21312 },
{ id: 2, message: 'Have you ever been to Georgia?', likesCount: 31312312 },
],
newPostText: 'q',
profile: null
};
const profileReducer = (state = initialState, action) => {
switch (action.type) {
case ADD_POST:
let newPost = {
id: 124331,
message: state.newPostText,
likesCount: 0
}
return { ...state, posts: [...state.posts, newPost], newPostText: '' }
case UPDATE_NEW_POST_TEXT:
return { ...state, newPostText: action.newText };
case SET_USER_PROFILE:
return { ...state, profile: action.profile };
default:
return state;
}
}
export const addPostActionCreator = () => ({ type: ADD_POST });
export const updateNewPostTextActionCreator = (text) => ({ type: UPDATE_NEW_POST_TEXT, newText: text });
export const setUserProfile = (profile) => ({ type: SET_USER_PROFILE, profile });
export default profileReducer;
您必须在代码中使用async/await。 看看这篇文章:
您将在那里找到相同的代码。state.profilePage未定义。除非您还有一些代码没有显示,否则您不会在上面代码的任何地方设置profilePage,因此它将始终是未定义的。我应该在这里显示什么?我的意思是,我在UsersContainer和usersReducer中做了同样的事情,一切都正常了
让MapStateTops=(state)=>({profile:state.profilePage.profile})代码>您所在的州没有profilePage-您所在的州有:posts、newPostText、profile。它始终是未定义的。