为什么我不能为变量分配响应?

时间:2019-12-11 21:01:55

标签: reactjs

我正在尝试在componentDidMount中将此通用变量分配给API调用的响应。如果我console.log(response['general'];),我可以看到响应对象中填充了我需要的数据,但是当我尝试将response['general']分配给变量generalconsole.log(general)时,其中没有任何内容。它,只有一个空对象。

componentDidMount() {
    var general = {};
    this.getSettingsForms().then((response) => {
        general = response['general'];
        this.setState({settingsForms: response})

    })
    console.log(general);
    this.getConfig("general", general)
}

getSettingsForms() {
    /**
     *  Integrator API call to retrieve a form schema for a given destination.
     *  params: destination (String): The name of a destination in the format of the product_type from the form schema.
     *  returns: A Promise Object containing the form schema for the requested destination.
     **/

    return fetch(`/ui/settings`, {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json',
            'Accept': '*/*'
        },
    })
        .then(response => response.json())
        .then(json => (json))
        .catch((err) => {
            /* Todo - add better error handling here. */
            console.log('Fetch Error :-S', err);
        });
}

这与我不理解的承诺有关吗?

0 个答案:

没有答案