我有以下代码,该代码正在进行HTTP调用并返回promise
const res = simulations.getSimulationRunAssets({
path: this.props.commonProps.apiPath,
apiVersion: DEFAULT_API_VERSION,
simulationRunId
});
console.log('**** res', res);
res
.then(data => {
console.log('2SimulationRun populateSimulationRun getsimulation run assets data', data);
if (!data) {
return Promise.reject(new Error('No data received from the API.'));
}
控制台输出显示返回了prmoise:
**** res Promise {<pending>}
...
2SimulationRun populateSimulationRun getsimulation run assets data
(3) [{…}, {…}, {…}]
0
:
{id: 1, name: "H..."}
...
但是随后我在控制台中得到以下信息:
Unhandled Rejection (TypeError): Cannot read property 'Symbol(Symbol.iterator)' of undefined
在下一行右边(res.then(data => {):
res
> 91 | .then(data => {
getSimulationRunAssets:
const getSimulationRunAssets = ({ path, apiVersion, simulationRunId }) => {
const urlPath = `${path}${apiVersion}/simulation-runs/${simulationRunId}/assets`;
console.log('GET Simulation Run Assets API Call', `${urlPath}`);
const promise = axios
.get(urlPath)
.then(res => {
console.log('GET Simulation Run Assets Response', res);
if (res.status !== 200) {
const err = new Error('Error retrieving simulation run assets');
err.response = res;
throw err;
}
console.log('getSimulationRunAssets returning res.data', res.data);
return res.data;
})
.catch(err => {
console.error(err);
if (err.response && err.response.data && err.response.data.message) {
err = new verror.VError(err, err.response.data.message);
}
console.log('Rejecting');
console.log('getSimulationRunAssets returning rejecting', err);
return Promise.reject(err);
});
console.log('getSimulationRunAssets returning promise', promise);
return promise;
};
答案 0 :(得分:0)
Chrome中的错误不是很有帮助。当我切换到Firefox时,我可以看到该错误来自getSimulationRunAssets()。 Firefox错误表示未定义arr。
[currentAsset] = data.name;
应该是:
currentAsset = data[0].name;