axios 获取请求返回未定义对象

时间:2021-03-18 14:19:50

标签: node.js reactjs axios

我有以下代码用于 React 应用程序中的索引文件

componentDidMount() {
        const { id, tenant_account_id } = this.props.match.params;
        this.props.getUserView(id, tenant_account_id);
    }

    async componentDidUpdate(nextProps) {
        const { id, tenant_account_id } = this.props.match.params;
        if(!this.props.user && nextProps.user) this.props.getUserView(id, tenant_account_id);
        await this.receiveApiData();
    }

    receiveApiData() {
        const { id } = this.props.match.params;
        if (typeof this.props.userDetails !== "undefined" && this.props.userDetails !== null && this.props.userDetails.id === id && !this.state.dadosApi) {
            this.setState({ id: this.props.userDetails.id });
            this.setState({ name: this.props.userDetails.name });
            this.setState({ email: this.props.userDetails.email });
            this.setState({ dadosApi: true });
        }
    }

此应用程序通过此代码查询操作以获取给定帐户 (tenant_account_id) 的给定用户 (id) 的信息。

export const getUserView = (id, tenant_account_id) => {
    return function (dispatch) {
        axios.get(api + `/user/${id}/${tenant_account_id}`, getHeaders())
        .then((response) => {
            dispatch({type: GET_USER, payload: response.data});
        })
        .catch(errorHandling)
    }
}

但是,在控制台上运行应用程序和咨询时,调用 this.props.userDetails 返回“未定义”。我的错误是什么,我该如何解决?

0 个答案:

没有答案