我正在尝试在componentDidMount
中将此通用变量分配给API调用的响应。如果我console.log(response['general'];)
,我可以看到响应对象中填充了我需要的数据,但是当我尝试将response['general']
分配给变量general
和console.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);
});
}
这与我不理解的承诺有关吗?