基于url从firestore中检索数据

我正在尝试使用从firestore获得的基于URL内容的数据来呈现配置文件页面。例如,当用户输入mywebsite/profile/someusername时,我希望从firestore数据库中检索配置文件信息并呈现Profile组件。我有一个保存应用程序状态的redux存储,我有postReducerauthReducer以及各自的操作。当我使用componentDidMount()生命周期方法进行第一次渲染时,当用户转到他们自己的个人资料页面时,它会显示关于他们的信息和他们自己的数据。但是一旦在URL上输入其他人的用户名,我就会得到像cannot read property 'props' of undefined这样的错误。对我来说,什么是一个好的策略?任何帮助都将不胜感激。我的Profile组件代码:

class Profile extends Component {
    //component state and render method are removed to make some space

    componentDidMount() {
        this.props.getUserPosts(this.props.profile.username);
    }
}

const mapStateToProps = (state) => {
    return {
        auth: state.firebase.auth,
        profile: state.auth.profile,
        profileError: state.auth.profileError,
        userPosts: state.post.userPosts,
        userError: state.post.userError
    }
};
const mapDispatchToProps = (dispatch) => {
    return {
        getUserPosts: (username) => {
            dispatch(getUserPosts(username));
        },
        getProfile: (username) => {
            dispatch(getProfile(username));
        }
    }
};
export default connect(mapStateToProps, mapDispatchToProps)(Profile);

转载请注明出处:http://www.xhjyjj.com/article/20230526/2559111.html