【发布时间】:2019-07-01 18:35:43
【问题描述】:
我在使用 axios 时遇到了一些 Flow 错误。
Cannot call await with 'axios.get(...)' bound to 'p' because:
Either property 'error_message' is missing in 'AxiosXHR'.
Or property 'data' is missing in 'Promise'
这是我的代码,尝试了类型注释。 (同样的错误没有AxiosPromise<Object> 注释。)错误在axios.get(url)。
async handleAddressChange(): AxiosPromise<Object> {
const url = `https://maps.googleapis.com/maps/api/place/autocomplete/json?key=${GoogleMapsApiKey}&input=${this.state.address}`;
try {
const { data, error_message } = await axios.get(url);
if (error_message) throw Error(error_message);
this.setState({
addressPredictions: data.predictions,
showPredictions: true
});
} catch (err) {
console.warn(err);
}
}
有趣的是,在另一个文件中 axios 没有给出 Flow 问题:
export async function loginWithApi(creds: AuthParams) {
const res = await axios.get(ApiUrls.login, { params: creds });
return res.data;
}
我的文件中有import type { AxiosPromise, $AxiosXHR } from "axios";。
有人知道如何解决这个问题吗?
【问题讨论】: