【发布时间】:2020-12-10 14:48:36
【问题描述】:
我试图从 componentDidMount() 生命周期方法的公共文件夹中将数据加载到我的项目中。但是,我没有得到想要的 FeatureCollection 对象,而是一个待处理的 Promise。
componentDidMount = () => {
...
const data = fetch(`vcd/${this.state.monthFile}`)
.then(response => response.text())
.then(async data => {
return csv2geojson.csv2geojson(data, {
latfield: 'lat',
lonfield: 'lng',
delimiter: ','
}, (err, data) => {
if (err) console.log(err);
console.log(data); // correctly outputs a FeatureCollection, length 30277
return data;
// this.setState({ someAttribute: data }) => Also doesn't work.
})
})
.then(data => data); // If to use another Promise chaining, the result would be undefined.
console.log(data); // a pending Promise
}
我的文件包含 30277 行 * 3 列,大小约为 500Kb,我认为这不应该是数据加载的问题,在咨询了 csv2geojson 和 fetch API 之后,我仍然想不出一个解决方案问题。我很感激任何有用的意见。
编辑:同时使用 async-await 模式并链接另一个 .then 将导致 undefined。
【问题讨论】:
-
为什么
this.setState({ someAttribute: data })在承诺链中不起作用?