【发布时间】:2021-10-25 06:32:41
【问题描述】:
我希望 Axios 调用的结果(console.log 所在的位置)进入它下面的对象(location_geo_point: 所在的位置)。我尝试将 getCoords 函数放在 createElasticSearchJob 函数之外,并将结果保存在 state 中。但是这不起作用,因为我将 {location} 传递到 url 并且它还没有被声明。
function createElasticSearchJob(rowData, getCoords) {
function getCoords() {
return axios
.get(
`http://nominatim.openstreetmap.org/search?q=${location}&format=json&polygon=1&addressdetails=1`
)
.then((res) => {
console.log(res.data[0]);
});
}
const {
country,
hiring_company: {
name: companyName
},
id,
location,
name,
posted_time,
posted_time_friendly,
snippet,
state: locationState,
url,
} = rowData;
return {
job_id: id,
job_title: name,
location: location,
num_days_since_posted: posted_time_friendly,
job_date_posted: posted_time,
job_date_posted_date_value: posted_time,
job_url: url,
job_title_full_from_desc: name,
job_company_name: companyName,
job_location: location,
job_desc_text: snippet,
job_desc_html: snippet,
job_source: "ZipRecruiter API",
job_years_of_experience: null,
DOC_HASH: id,
location_geo_point: getCoords(),
};
}
【问题讨论】:
标签: javascript reactjs json object axios