Reactjs 如何使用地理定位API调用异步方法?
在我的React应用程序中,我希望能够使用用户的当前位置调用API端点 我有以下方法,当用户单击按钮时触发Reactjs 如何使用地理定位API调用异步方法?,reactjs,async-await,fetch,Reactjs,Async Await,Fetch,在我的React应用程序中,我希望能够使用用户的当前位置调用API端点 我有以下方法,当用户单击按钮时触发 findMeetings = async () => { const location = window.navigator && window.navigator.geolocation if (location) { location.getCurrentPosition((position) => { let url = `me
findMeetings = async () => {
const location = window.navigator && window.navigator.geolocation
if (location) {
location.getCurrentPosition((position) => {
let url = `meetings/find?lat=${position.coords.latitude}&lng=${position.coords.longitude}&radius=${this.state.radius}`;
let result = await fetch(`https://localhost:44303/api/${url}`, {
method: 'get'
}).then(response => response.json());
this.setState({ meetings: result.meetings })
}, (error) => {
})
}
我得到以下错误:
分析错误:wait是保留字
我想这是因为我在
getCurrentPosition
中调用了一个异步方法-有人能帮我理解这里的问题是什么以及如何解决它吗?这可能是因为即使父方法有关键字async(findMeetings),location.getCurrentPosition(…)函数没有异步关键字
因此,尝试这样的方法可能会有所帮助:
location.getCurrentPosition(async position => {
.. actual code
})
让我知道这是否有效,干杯:)很高兴我能帮忙!