当我console.log我的数组时,我可以看到它,但是当我尝试获取其中一个元素时,它返回的是不确定的。我读过几个类似的线程,它们说此问题可能是由于数组异步填充而导致的,所以尚未对结果建立索引,但是我正在使用asyn / await。我提供了console.logs的代码和屏幕截图。
_parseLocation = async (file : File) => {
const {campaign} = this.props;
const {locations} = this.state;
let addresses: Array<{name: string, address: string}> = [];
var i = 0;
var csvFilePath = file;
var Papa = require("papaparse/papaparse.min.js");
await Papa.parse(csvFilePath, {
header: true,
download: true,
skipEmptyLines: true,
step: function (row: { data: any; }) {
if (row.data.Zip) {
let firstName: string = row.data.FirstName
let lastName: string = row.data.LastName
let name: string = firstName + " " + lastName
let houseNumber: string = row.data.HouseNumber
let houseNumberSuffix: string = row.data.HouseNumberSuffix
let streetName: string = row.data.StreetName
let zip: string = row.data.Zip;
let address = houseNumber + " " + houseNumberSuffix + " " + streetName + " " + zip;
addresses.push({name, address})
}
}
});
return addresses;
}
_test = async (file: File) =>{
const { campaign } = this.props;
const { locations } = this.state
let addresses = await this._parseLocation(file)
console.log("addresses", addresses)
console.log("address", addresses[0])
}